Java – is a popular programming language, created in 1995.
Java comment- used to explain java code, and to make it more readable. (//)
Different types of variable
String – store text, such as “hello”. String values are surrounded by double quotes
Int – stores integers (whole numbers), without decimals. Such as 123 or -123
Float – stores floating number, with decimals, such as 19.99 or 19.99
Char- stores single characters, such as ‘a’ or ‘b’. Char values are surrounded by single quotes
Boolean- stores values with two state: true or false
Primitive number types are divided into two group
Integer types- store the whole number, positive or negative, without decimals
Valid types are byte, short, int and long. Which type you should use, depend on the numeric value
Floating point types – represent numbers with a fractional part, containing one or more decimals.
These are two types : float and double
Non primitive
Java Type of casting
Widening Casting (automatically) - converting a smaller type to a larger type size
Byte> short>char> int> long> float> double
Narrowing Casting (manually) – Converting a larger type to a smaller size type
Double> float> long> int> char> short> byte>
OPERATORS
JAVA STRING
STRING LENGTH
• A String inn java is actually an object, which contain methods that can perform certain
operation on string. For example, the length of a string can be found with length()
method:
STRING CONCATENATION
• The + operator can be used between strings to add them together to make a new string.
JAVA CONDITION AND IF STATEMENT
Java support the usual logical condition from mathematics:
• Less than: a < b
• Less than or equal to: a <= b
• Greater than: a > b
• Greater than or equal to : a >= b
• Equal to : a == b
• Not equal to : a ! = b
Java has the following conditional staetment
• Use if to specify a block of code to be executed, if a scpecified condition is true
Ex.
• Use else to specify to specific a block of code to be executed, if a scpecified condition is false
Ex.
• Use else if to specify a new condition to test, if the first condition is false
Ex.
• Use Switch tp specify many alternative blocks of code to be executed
- The value of the expression is compared with the value of each case
- If there is a match, the associated block of code to be executed
- The break and default keywords are optional.
DEFAULT KEYWORDS
- Specifies some code to run if there is no case match
JAVA WHILE LOOP
Loops can execute a block of code as long a specified condition is reached.
The While loop loops through a block of a code as long a specified condition is true
JAVA DO/WHILE LOOP
- is a variant of the while loop. This loop will execute the code block once, before
checking if the condition is true, then it will repeat the loop as long the condition
is true.
FOR LOOP
- When you know exactly how many times you want to loop throung a block of code, use for loop
instead of a whileloop.
For each LOOP
- which is used exclusively to loop through elements in a array