Java Technology 2018 - Basics 1 - Operators

If you have a file Simple.java
class Simple{ 
 public static void main(String args[]){  
     System.out.println("Hello Java");  
    }  
}  

To compile:javac Simple.java
To execute:java Simple

Understanding first java program

Let's see what is the meaning of class, public, static, void, main, String[], System.out.println().
  • class keyword is used to declare a class in java.
  • public keyword is an access modifier which represents visibility, it means it is visible to all.
  • static is a keyword, if we declare any method as static, it is known as static method. The core advantage of static method is that there is no need to create object to invoke the static method. The main method is executed by the JVM, so it doesn't require to create object to invoke the main method. So it saves memory.
  • void is the return type of the method, it means it doesn't return any value.
  • main represents startup of the program.
  • String[] args is used for command line argument. We will learn it later.
  • System.out.println() is used print statement. We will learn about the internal working of System.out.println statement later.

Variables and Data Types in Java 

Variable is name of reserved area allocated in memory. In other words, it is a name of memory location. It is a combination of "vary + able" that means its value can be changed.
variables in java

Types of Variable

There are three types of variables in java:
  • local variable
  • instance variable
  • static variable

1) Local Variable

A variable which is declared inside the method is called local variable.

2) Instance Variable

A variable which is declared inside the class but outside the method, is called instance variable . It is not declared as static.

3) Static variable

A variable that is declared as static is called static variable. It cannot be local.

class A{  
int data=50;//instance variable  
static int m=100;//static variable  
void method(){  
int n=90;//local variable  
}  
}//end of class  


Data Types in Java

Data types represent the different values to be stored in the variable. In java, there are two types of data types:
  • Primitive data types
  • Non-primitive data types
datatype in java


Data TypeDefault ValueDefault size
booleanfalse1 bit
char'\u0000'2 byte
byte01 byte
short02 byte
int04 byte
long0L8 byte
float0.0f4 byte
double0.0d8 byte

Operators in java

Operator in java is a symbol that is used to perform operations. For example: +, -, *, / etc.
There are many types of operators in java which are given below:
  • Unary Operator,
  • Arithmetic Operator,
  • shift Operator,
  • Relational Operator,
  • Bitwise Operator,
  • Logical Operator,
  • Ternary Operator and
  • Assignment Operator.



Operator TypeCategoryPrecedence
Unarypostfixexpr++ expr--
prefix++expr --expr +expr -expr ~ !
Arithmeticmultiplicative* / %
additive+ -
Shiftshift<< >> >>>
Relationalcomparison< > <= >= instanceof
equality== !=
Bitwisebitwise AND&
bitwise exclusive OR^
bitwise inclusive OR|
Logicallogical AND&&
logical OR||
Ternaryternary? :
Assignmentassignment= += -= *= /= %= &= ^= |= <<= >>= >>>=

class OperatorExample{  
public static void main(String args[]){  
System.out.println(10<<2);//10*2^2=10*4=40  
System.out.println(10<<3);//10*2^3=10*8=80  
System.out.println(20<<2);//20*2^2=20*4=80  
System.out.println(15<<4);//15*2^4=15*16=240  
}}  

class OperatorExample{  
public static void main(String args[]){  
System.out.println(10>>2);//10/2^2=10/4=2  
System.out.println(20>>2);//20/2^2=20/4=5  
System.out.println(20>>3);//20/2^3=20/8=2  
}}  

Java AND Operator Example: Logical && and Bitwise &

The logical && operator doesn't check second condition if first condition is false. It checks
 second condition only if first one is true.
The bitwise & operator always checks both conditions whether first condition is true or false.

class OperatorExample{  
public static void main(String args[]){  
int a=10;  
int b=5;  
int c=20;  
System.out.println(a<b&&a<c);//false && true = false  
System.out.println(a<b&a<c);//false & true = false  
}} 

Java OR Operator Example: Logical || and Bitwise |

The logical || operator doesn't check second condition if first condition is true. It checks second condition only if first one is false.
The bitwise | operator always checks both conditions whether first condition is true or false.



  1. class OperatorExample{  
  2. public static void main(String args[]){  
  3. int a=10;  
  4. int b=5;  
  5. int c=20;  
  6. System.out.println(a>b||a<c);//true || true = true  
  7. System.out.println(a>b|a<c);//true | true = true  
  8. //|| vs |  
  9. System.out.println(a>b||a++<c);//true || true = true  
  10. System.out.println(a);//10 because second condition is not checked  
  11. System.out.println(a>b|a++<c);//true | true = true  
  12. System.out.println(a);//11 because second condition is checked  
  13. }}  

Java Ternary Operator Example

  1. class OperatorExample{  
  2. public static void main(String args[]){  
  3. int a=2;  
  4. int b=5;  
  5. int min=(a<b)?a:b;  
  6. System.out.println(min);  
  7. }}  

Comments

Popular posts from this blog

Jersey JAX RS Jar

Tomcat Installation Steps for Windows & add tomcat to eclipse

REST API