Java Programming Data Type Operators Decision Statement and Loops

-- Leave a Comment
Hello, welcome back we are here with the next article of Get Started with Java Programming. Here we would be talking about Data Type, Operations, Decision Statement and loops. Java supports boolean data types as a primitive data type.

Data Type

Data type specify the the size and type of the values to be stored in the variable. Java have lots of data types which allow programmers to select appropriate data type they needed. In Java we can categorise data types into "Primitive types" and "Derived types". We will be discussing about build-in types(Primitive).

Integer Types

Integer can hold whole numbers. Java supports four type of integers, bytes, short, int, long. The size of the values that can be stored in a variables depends upon the type of integer. In the table below we specify Types size and value range:-
Type Size Minimum Value Maximun value
bytes One byte -128 127
short Two bytes -32768 32767
int Four bytes -2147483648 2147483647
long Eight bytes -9223372036854775808 9223372036854775807

Floating Point Types

floating point types can hold number combination fractional numbers. In Java there are two types of floating point integer float and double. In the table below range and size of these two types are specified. Floating points supports a special value know as Not-a-Number(NaN). NaN is used to represent the result of operation as dividing by zero. Float are of 4 bytes whereas double are of 8 bytes.

Other Types:

char:- Char is used to store character value in Java program.  This type is of 2 bytes but can hold only one single character.
boolean: In Java boolean is accepted as a primitive data type. Whenever we have to test a condition during the execution of the program this can be used. There are only two possible values of boolean type:- true or false. This use only one bit of storage.


A operation is symbol that tells the computer to perform certain mathematical or logical operations. we can classified into a numbers of categories:-
Arithmetic:- Used to conduct mathematical expressions as in algebra. +, -, *, /, % We can use Arithmetic operations on any build in data type except boolean type.
Lets Learn more about % operator:- the % operator will gives the remainder of the number. a % b will give the remender number (the remember while dividing a by b). The symbol of the value of the % will be based on the numerator.
10 % 3 == 1
10 % -3 == 1
-10 % 3 == -1
-10 % -3 == -1

Logical Operator:- && (and), ||(Or), !(Not). Logical operator will yield a value either true or false.
Var1 Var1 Var1! Var1 && Var1 Var1 || Var1
ture ture false ture ture
ture false false false ture
false ture ture false ture
false false ture false false

Relational Operator:- > , < , >= , <= , != Relation operator is used to compare two quantities. We can use these operation on selecting some task whenever some condition is satisfied.
Assignments Operator:- = , -= , += , *= , /= , %=
a +=b is similar to a = a + b
Equality:- == To show that thow of them are equal.
Unary:- ++ , -- Used for increment or decrement A = A + 1 is similar to A++
Ternary (conditional):- ?: Used as in if else condition. (Expression)?(Do this if true):(else do this);

Decision Statements

Decision statement are to make decision the the basic of the condition whether it is true or false. Java supports two discussion statements.
1. if statement
2. switch statement

if statement

if statement is used in decision making during the execution of the program. The if statement take of the following form:
if (condition)
If the condition is true the statement will be executed else it will be skipped. Depending upon the complexity of the program if can takes various form as below:
  • if statement
  • if .. else statement
  • nested if else statement
  • else if statements 

Switch Statements

In switch statement statements are executed upon the required case are true. Switch case takes the following form.
switch (case){
case 1:
The program will check for the value of the variable case if it matches 1 statement1 will be executed if 2 statement2. we can define the default option in switch case too in case non case satisfied the statement in the default will be executed.

We can use any data types in if statements and any expression are accepted in if statements. But we can only make equality comparison on switch and only accept integer, char and Strings (allowed by Java SE 7).

Looping in Java

Java supports all the three looping namely while, do while and for loop. Loops are used when we need to execute certain part of the program repitately. Whenever a loop check the condition while entering in the loop such loop is known as entry control loop. When a loop checks the condition at exit such looping is known as exit control loop. While and for loop are entry control loop where as do while is exit control loop. We can set certain number of turns or we can set till some conditions happens.

Hello This is Sagar Devkota. Studying Bachelor of Software Engineering at Pokhara University, NCIT. Open Source Enthusiastic.


Post a Comment