Beruflich Dokumente
Kultur Dokumente
byte:
-129 to 127
short: -32768 to 32767
long: (add L to the back of it) -263 and a maximum value of 263-1
Float has 7 decimal digits of precision
Double has 16 decimal digits of precision. Is normally used and double is less popular because
it is not as precise.
Char saves a single character. Can also save unicode characters char myChar = '\u00A9';
boolean used for conditional logic (true or false)
You can create your own data types and combine primitive types inside. (called Objects)
A string is a sequence of characters. Unlike char that can contain single character or unicode,
string can contain much more than that.
Operators
Operator in java is a symbol that is used to perform operations. There are many types of
operators in java such as unary operator, arithmetic operator, relational operator, shift operator,
bitwise operator, ternary operator and assignment operator.
+
%
When checkig conditions if ( xxx == true)
sout;
== equal to
!= not equal to
>
<
&& (and)
|| - OR siwa honda
At its most basic, the ternary operator, also known as the conditional operator, can be used as
an alternative to the Java if/then/else syntax, but it goes beyond that, and can even be used on
Expressions
Building blocks of all java programs built with values, method calls, operators and values.
you can create variables inside code blocks and reference existing variables but once the code
block runs the created variable gets deleted. Concept is called Scope.
break;
This will check for the value after case keyword. Break tells it to move on after. Then
you can have case 2: and so on. When you want to end it and display the value for
anything else but the checked things type in
default:
do something
break;
To account for all scenarios without having to check all possible upper and lower
case variations you can use the method called .tolowercase
Example
Switch (month.tolowercase())
for
Often referred to as the forLoop.
for(init; termination; increment)
package com.teodor;
public class Main {
public static void main(String[] args) {
int count = 0;
for (int i=10; i<=50; i++ ) {
if (isPrime(i)) {
count++;
System.out.println("It is a prime number equal to " + i);
if(count==5) {
break;
}
}
}
}
//This code checks whether the number is not prime and sets the value of the
method to true if it is prime.
public static boolean isPrime(int n) {
if (n == 1) {
return false;
}
for (int i=2; i <= (long) Math.sqrt(n); i++) {
if (n % i ==0) {
return false;
}
}
return true;
}
Class
Variables inside a method are local and they cannot be accessed outside that
method. Classes allow us to create variables that can be seen and are accessible
from anywhere within the class we are creating. Known as fields.
public unrestricted access to the class from other classes; private no other
class can access that class; protected only classes within this package can
access.