vivarem logo
/ Vivarem / Computers and IT / IT Education / online / tutorial / Java / OOP / 2 /
There are 0 sub sections

You can add Your Link here with New Link
Java Object Oriented Programming - 2

Primitive Types and Strings and operators

java language has the following primitive types
int - integer type with 4 bytes storage allocation.
short - integer type with 2 byte storage allocation.
long - integer type with 8 byte storage allocation.
double - decimal type with 8 bytes storage allocation
float - decimal type with 8 bytes storage allocation
char - stores a single char with 2 byte storage making it capable of storing unicode character.Basically they are 2 byte unsigned characters.
byte - stores a single byte
boolean can store true or false values single bit.
To store hexadecimal value use 0x preceeding the hexadecimal characters
String is not a primitive type but it is used as much as if not more than many primitive types. It can store a sequence of characters.

Operators
On all numeric literals you can perform intereger operation with operators as follows.
= :- assignment
+ :- add
- :- substract
* :- multiply
/ :- divide
% :- mode (remainder)
++ :- unary increment, will increment the variable by one
-- :- unary decrement will decrement the variable by one
In addition you can use operators in combination with the assignment operators.
+= :- increment the variable by a specific number and assign it to the variable.
-= :- decrement the variable by a specific number and assign it to the variable.
*= :- multiply and assign
/= : divide and assign
%= : get the remainder and assign

Shift operators
<< left shift
>> right shift

Comparison operator
== equals
< lessthan
> greater than
<= lessthan or equal
>= greaterthan or equal
!= not equal

Logical operators
&& :- AND, true ony when both are true
|| :- OR, true only both or either is true
^ :- XOR, true when any one is true
! :- NOT, true when false

package com.vivarem.examples;
public class JavaTypes{
public static void main(String[] arg){
int a=10;//4 byte long
long b=10L;//8 byte long
double c=10.50;//64 byte long can handle decimals
float f=10f;//64 byte long can handle decimal
char ac='\u00f0';// supports unicode 2 byte long
byte by=4;// 1 byte long
short sh=0xff;//2 byte long storing a hexadecimal

byte by1=0xe;
System.out.println("int a="+a+" long b="+b+" double c="+c+" float f="+f+" char ac="+ac+" byte by="+by+" short sh="+sh+" by1="+by1);
int al=ac;
System.out.println(al);
String s="Hello World";
s+=" how are you";
System.out.println(s);
//numeric operands
a=10+2;System.out.println(a);
a=10-2;System.out.println(a);
b=10*2;System.out.println(b);
System.out.println(b++);// prints and increments
System.out.println(++b); //increments and prints
System.out.println(b--); //prints and decrements by one
System.out.println(--b); //decrements by one and prints
System.out.println(11%2);// prints the remainder of 11/2 =>1
System.out.println(10/2);//divide by 2
System.out.println(11>>2); //shift right by 2
System.out.println(11<<11);//shift left by 2
System.out.println("bitwise operation");

System.out.println("bitwise and 2&3="+(2&3));
System.out.println("bitwise or 2|3"+(2|3));
System.out.println("bitwise xor 2^3"+(2^3));
/**control structures comparision operators
< lessthan
> greaterthan
== equals
<= lessthan or equalto
>= greaterthan or equalto
!= not equalto
this is a nested if elseif statementi the last else statement is never reached.
**/
if(a>b){System.out.println(a+">"+b);}
else if (a==b)
System.out.println(a+"=="+b);
else if (a<b)
System.out.println(a+"<"+b);
else
System.out.println(a+" !="+b);

System.out.println("(a<b)?a:b"+((a<b)?a:b));
}
}



< Java Class an Introduction |
Content |
Control Structures >


You can Add a Classified here with New Classified