Sie sind auf Seite 1von 6

Variables & Datatypes

Primitive Types & String

integer - a whole number without decimals


value can be between -2147483648 and 2147483648

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

int topScore = 99;


if (topScore == 100)
System.out.println("You got the high score!");
int secondTopScore = 60;
if ((topScore > secondTopScore) && (topScore < 100))
System.out.println("True that!");

int topScore = 100;


if (topScore == 100)
System.out.println("You got the high score!");
ternary operator
In a single line of code, the Java ternary operator let's you assign a value to a variable based on
a boolean expression either a boolean field, or a statement that evaluates to a boolean result.

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

the right hand side of Java statements.

result = testCondition ? value1 : value2

Expressions

Building blocks of all java programs built with values, method calls, operators and values.

If Keyword and Code Blocks

if(gameOver == true) (can be abbr to if(gameOver) {


int finalScore = score + (levelCompleted * bonus);
System.out.println("Your final score was " + finalScore);
}

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.

Control Flow Statements


Switch
Good to use when testing the same variable against different values. Switch
statement can be used with four primitive statements: byte, short, char and int.
Since Java 7 we can use String as well.
Switch(insertvariableyoumadebeforehere) {
case 1:
System.out.println(text);

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;

Example with String


String month = January
switch (month) {
case January

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)

init initialization (initialize a variable for example int i=0)


terminations has to be false before the loop can finish.
increment at the end of each loop increases the value of the variable we set
initially
for(int i=0; i<5; i++) {
System.out.println("Loop " + i + " hello!");
}

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;
}

while and do while statements


Used when we dont know how many times we want to loop exactly.
int count = 1;
while(count !=5) {
System.out.println("Count value is " +count);
count++;
}

do (while) is always going to be executed at least once.

Object Oriented Programming


Software objects consists of states and behaviors. States are fields and behaviors
are methods. A class can be thought of as a template or a blueprint for creating
objects.

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.

Das könnte Ihnen auch gefallen