JAVA Chapter II

man-using-computerHi people, this blog is going to discuss about very basic points and main concepts you must know before using JAVA language. This second article will be helpful to build the foundation of JAVA programming. In this tutorial we will learn about:-

  • Classes & Objects.
  • Operators & concatenation.
  • IF statement.

Classes & Objects

  • Entire Java language is depends on classes and objects. Without the knowledge of classes and objects you can’t even think about coding.
  • To understand what are a class and an object I’m going to take a simple example.
  • A class is like a blueprint and an object is like a copy of that blueprint.
  • Ex: – If Human being is a class, one person is an object of the Human being class.

If Desk is a class, one desk is an object of Desk class.

Classes

  • A class has a set of attributes (instance variables) and set of operations. (methods)
  • Java has 4 access levels for protection purposes. Those are known as access specifies.
  1. public
  2. protected
  3. default
  4. private
  • Classes can use only two access specifies. Those are public & default.

Public class

1

Default class

2

  • Also a class can contain constructors and inner classes. (These will be discussed in future articles).
  • Java has 4 types of variables.
  1. Instance variables – outside the method and inside the class.
  2. Local variables – inside a method or a block.
  3. Static variables – variables that declare with static key word. (outside the method)
  4. Constant variables – variables that has constant value. (Mostly declare with final key word and definitely outside the method).

Objects

  • The keyword new uses for create an object.
  • Object class is the super most class in the java. (In future articles we will discuss about super class).
  • Ex: – If we take Human being is a class and one person is an object example,

HelloWorld class

3

HumamBeing class

4

5

Important

  • Static variables – one copy per class – share through all objects in the class.
  • Instance variables – one copy per object
  • If we take Circle as a class,

6  7

  • This is only basic knowledge. We will discuss furthermore about class, inner class, objects, constructors, and methods in future articles.

Operators & concatenation

Operators

10

  • There are few types of operators.
  • Unary operators.
  • Binary operators. (normal operators with 2 operands, ex:- 2+3 )
  • Ternary operators. (normal operators with 3 operands, ex:- 2+3-1 )
  • Arithmetic assignment operators.
  • Comparison operators.
  • Logic gates.

Unary operators

  • Only has one operand. (++1,–25 …)
  • Only has increment operator (++) and decrement operator (–).
  • There are two notations of unary operators.
  1. Prefix notation

Operator comes before the operand.

Ex 1:- int x = 1;  //create a integer variable x and assign value 1.

int y = ++x;  //create another integer variable y and assign ++x (prefix increment).

  • First x increase by 1.
  • After increased x (x=2) assign to the y.
  • Finally x=2, y=2

 

Ex 2:- int x = 1;  //create a integer variable x and assign value 1.

int y =x;  //create another integer variable y and assign –x (prefix decrement).

  • First x decrease by 1.
  • After decreased x (x=0) assign to the y.
  • Finally x=0, y=0

 

  1. Postfix notation

Operator comes after the operand.

Ex 1:- int x = 1;  //create a integer variable x and assign value 1.

int y = x++;  //create another integer variable y and assign x++ (postfix increment).

  • First value of x (x=1) assign to the y.
  • After x increase by 1.
  • Finally x=2, y=1

 

Ex 2:- int x = 1;  //create a integer variable x and assign value 1.

int y = x;  //create another integer variable y and assign x– (postfix decrement).

  • First value of x (x=1) assign to the y.
  • After x decrease by 1.
  • Finally x=0, y=1

(Scroll down for the example code)

Arithmetic assignment operators

  1. Assignment

X = 5;  //assigning value 5 to x using = operator

 

  1. Equality

(X == 5)   //check whether value of x is 5 or not using == operator

  1. Other

x += 5;  //this mean  is x = x+5

x *= 5; //this mean  is x = x*5

(Scroll down for the example code)

Copmparison operators

  • H.S – left hand side, R.S.H. – right hand side

L.H.S == R.S.H. (check equality)

L.H.S != R.S.H. (check not equality)

L.H.S > R.S.H. (check whether L.H.S is greater than R.S.H.)

L.H.S < R.S.H. (check whether L.H.S is less than R.S.H.)

L.H.S >= R.S.H. (check whether L.H.S is greater than and equal R.S.H.)

L.H.S <= R.S.H. (check whether L.H.S is less than equal R.S.H.)

(Scroll down for the example code)

Logic gates

  • We use two logic gates in java.

 

  1. AND

(x&& x >10) – It behave as normal AND logic gate.

 

  1. OR

(x|| x >10) – It behave as normal OR logic gate.

 

(Scroll down for the example code)

 

Important

  • Do not use >= as => and <= as =<, every time = sing comes after < > these two.

 

Concatenation

  • In java we use + sign to combine string with another string or variable for display purpose. It is known as concatenation.

(Scroll down for the example code)

If statement

  • The division box in flowchart is replaced with “if” statement in java.
  • In java we mostly use “if” statement to check some condition whether it is correct or wrong.

if(condition) {

True

} else {

False

}

(Scroll down for the example code)

Coding Example

8

  • I didn’t mention values of variable line by line. Try to do it. (Dry run).

Console

9

JAVA Chapter I               JAVA Chapter III

Advertisements

4 Replies to “JAVA Chapter II”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s