Quick introduction to Java Programming language - Part 1
Chapters
Operators in Java
Operator is a symbol that is used to do some specific operations.Types of operators in java:
- Arthematic Operators
- Relational Operators
- Logical Operators
- Assignment Operators
- Comparison Operators
- Unary Operators
Arthematic Operators:
These operators are used to perform athematic operations like addition, substraction, division etc.We will get the number as result from this operations.
Here “+” symbols is also used for concatenation purpose.If any operand is string then “+” acts as a concatenation.
Example:
10+30 = 40
"abc"+10 ="abc10"
"ab”+"cd"="abcd"
Example:
public class AddNumbs {
public static void main(String[] args){
int a =10;
int b = 8;
int sum=a+b;
System.out.println("the sum of two number is:" + sum); //here, “+” is acting as concatenation
}
}
Relational Operators:
These operators is used to compare the relation between two variables. We will get the boolean values(true/false) as result from this operation.
Relation Operators are:
Operators |
Description |
> |
Greater than |
< |
Lesser than |
>= |
Greater than or equal |
<= |
Lesser than or equal |
== |
Comparison |
!= |
Not equal |
= |
Assignment |
Example:
public class Example {
public static void main(String[] args){
int a =10;
int b = 8;
System.out.println( a == b ); // returns false
System.out.println( a != b ); // returns true
}
}
Logical Operators:
These operators takes two boolean inputs and we will get one boolean output.Logical Operators are:
Operators |
Description |
&& |
Logical AND |
|| |
Logical OR |
! |
Logical NOT |
The AND works: | The OR works: | The NOT works: |
True && True = True True && False = False False && True = False False && False = False |
True || True = True True || False = True False || True = True False || False = False |
!True = False !False = True |
Example:
public class Example {
public static void main(String[] args){
int a =10;
int b = 8;
System.out.println( (a >= 9) && (b <= 10)); // returns true
System.out.println( (a > b) || (b> 10) ); // returns true
}
}
Assignment Operators:
These operator is used to assign the values.Assignment Operators are:
Operators |
Description |
= |
Assignment operator |
/= |
Divide and assignment operator |
*= |
Multiply and assignment operator |
-= |
Subtract and assignment operator |
+= |
Add and assignment operator |
%= |
Modulus and assignment operator |
<<= |
Left shift AND assignment operator |
>>= |
Right shift AND assignment operator |
|= |
Bitwise inclusive OR and assignment operator |
&= |
Bitwise AND assignment operator |
^= |
Bitwise exclusive OR and assignment operator |
Example:
public class AddNumbs {
public static void main(String[] args){
int a = 0;
int b = 8;
a+=b; // a now have evalue 8
System.out.println("Value of a is :" + a);
}
}
Unary Opertors:
These operators will work for only one operand.
Types of Unary Operators:
- Increment ++;
- Decrement --;
Example: Increment ++
public class AddNumbs {
public static void main(String[] args){
int a = 0;
a++;
System.out.println("Value of a is :" + a); // 1
}
}
Example: Decrement --
public class AddNumbs {
public static void main(String[] args){
int a = 1;
a--;
System.out.println("Value of a is :" + a); // 0
}
}
Bitwise Operators:
Java provides bitwise operators and bit shift operations on integral types.
Bitwise Binary Operations:
Four bit operators in Java.
- ~ - Unary complement
- & - Bitwise and
- ^ - Bitwise exclusive or
- | - Bitwise inclusive or
- <<-Binary Left Shift Operator
- >>-Binary Right Shift Operator
- >>>-Shift right zero fill operator
Examples:
1010 & 0101 = 0000
1100 & 0110 = 0100
1010 | 0101 = 1111
1100 | 0110 = 1110
~1111 = 0000
~0011 = 1100
1010 ^ 0101 = 1111
1100 ^ 0110 = 1010
Conditional Operator:
are ternary operators which has three operands.
Syntax:
(Expression) ? (true) : (false);
Example:
int big = (a>b) ? a:b
Main method example
.....
public static void main(String args[])
{
int a=100;
int b = 20;
int big=(a>b)?a:b;
System.out.println(“big value:”+big); //output: big value:100
}
.....
Instanceof Operator:
These operatorused to check the object is of a particular type or not.This operator returnsBoolean value true/false.
Syntax:
(Object reference variable) instanceof (class type)
Example:
public class Test{
public static void main(String args[]){
String subject="Maths";
boolean result =subjectinstanceofString;//comparing the values
System.out.println( result ); // Output: True
}
}
In next chapter we will explore looping statements
Description
This tutorial is targetted for beginers seeking a quick get on with guide on Java programming language. 3 parts include
- Java basics
- Object oriented programming
- Advance concepts
Each part is subdivided in multiple chapters.
Java basics has the following chapters
- Introduction
- Environment Setup
- Basic Syntax
- Objects and Classes
- Basic Data Types
- Variable Types
- Modifier Types
- Basic Operators
- Loops
- Decision Making
- Numbers Class
- Character and String Class
- Arrays
- Date and Time
- Regular Expression
- Methods
- Streams, Files and I/O
- Exceptions Handling
Thanks for reading and as always, your feedback is very important to us. Let us know how we can improve and if you found any issues with this write up, send us a correction.
Audience
Beginners or students seeking a refresher on Java language
Author: Subject Coach
Added on: 9th Mar 2015
You must be logged in as Student to ask a Question.
None just yet!