You are on page 1of 47

Bi-Gems Technologies

JAVA
Java is a high-level programming language originally developed by Sun
Microsystems and released in1995.
Java runs on a variety of platforms, such as Windows, Mac OS, and the
various versions of UNIX
Object Oriented: In Java, everything is an Object. Java can be easily
extended since it is based on the Object model
Platform independent: Unlike many other programming languages
including C and C++, when Java is compiled, it is not compiled into
platform specific machine, rather into platform independent byte code

Bi-Gems Technologies

JAVA
Multithreaded: With Java's multithreaded feature, it is possible to write
programs that can do many tasks simultaneously.
High Performance: With the use of Just-In-Time compilers, Java enables
high performance
Simple:Java is designed to be easy to learn.
Secure: With Java's secure feature, it enables to develop virus-free,
tamper-free systems

Bi-Gems Technologies

Objects, Classes ,
Methods
Object - Objects have states and behaviors. Example: A dog has
states-color, name, breed as well as behaviors -wagging, barking, eating.
An object is an instance of a class.
Class - A class can be defined as a template/blue print that describes
the behaviors/states that object of its type support.
Methods - A method is basically a behavior. A class can contain many
methods. It is in methods where the logics are written, data is
manipulated and all the actions are executed.
Instance Variables - Each object has its unique set of instance
variables. An object's state is created by the values assigned to these
instance variables.

Bi-Gems Technologies

First Program

public class MyFirstJavaProgram{


/* This is my first java program.
* This will print 'Hello World' as the output
*/
public static void main(String[]args){
System.out.println("Hello World");// prints Hello
World
}
}

Bi-Gems Technologies

Basic Syntax:
Case Sensitivity: Java is case sensitive, which means identifier
Hello and hello would have different
Class Names - For all class names, the first letter should be in Upper
Case
Example class MyFirstJavaClass
Method Names - All method names should start with a Lower Case
letter.
myMethodName()
Program File Name - Name of the program file should exactly
match the class name
Assume 'MyFirstJavaProgram' is the class name, then the file should
be saved
as'MyFirstJavaProgram.java'
public static void main(String args[]) - Java program processing

Bi-Gems Technologies

Java Identifiers:
All Java components require names. Names used for classes, variables
and methods are called identifiers
All identifiers should begin with a letter (A to Z or a to z), currency character ($)
or an underscore (_).
After the first character, identifiers can have any combination of characters.
A keyword cannot be used as an identifier.
Most importantly identifiers are case sensitive.
Examples of legal identifiers:age, $salary, _value, __1_value
Examples of illegal identifiers: 123abc, -salary

Bi-Gems Technologies
Java variable:

Local Variables
Class Variables (Static Variables)
Instance Variables (Non-static variables)
Java Arrays:
Arrays are objects that store multiple variables of the same type
Java Enums:
Enums restrict a variable to have one of only a few predefined values

Bi-Gems Technologies

For example, if we consider an application for a fresh juice


shop, it would be possible to restrict the glass size to small,
medium and large.
Class FreshJuice{
enum FreshJuiceSize{ SMALL, MEDUIM, LARGE }
FreshJuiceSize size;
}
public class FreshJuiceTest{
public static void main(String args[]){
FreshJuice juice =new FreshJuice();
juice.size =FreshJuice.FreshJuiceSize.MEDUIM ;
}
}

Bi-Gems Technologies
Java Keywords:

Bi-Gems Technologies

Comments
public class MyFirstJavaProgram{
/* This is my first java program.
* This will print 'Hello World' as the output
* This is an example of multi-line comments.
*/
public static void main(String[]args){
// This is an example of single line comment
/* This is also an example of single line
comment. */
System.out.println("Hello World");
}
}

Bi-Gems Technologies

Java is an Object-Oriented
Language

Polymorphism
Inheritance
Encapsulation
Abstraction
Classes
Objects
Instance
Method
Message Parsing

Bi-Gems Technologies

Variable Declation & assigning


the value
public class Example2 {
public static void main(String[] args) {
int num;
// this declares a variable called num
num=10;

// this assigns num the value 100

System.out.println("This is num "+num);


num =num*2; //this is over riding the value of num
System.out.println("This is num *2 "+num*2);
}
}

Bi-Gems Technologies

The if Statement
public class IfClass {
public static void main(String[] args) {
int x,y;
x=10;
y=20;
if(x<y) System.out.println("x is less then y");
x=x*2;
if(x==y) System.out.println("x equal to y");
x=x*2;
if(x>2) System.out.println("x is now greater
than y");
}
}

Bi-Gems Technologies

For Loop

For Loop with block of statements

Bi-Gems Technologies

The Java Class Libraries


Javas built-in methods:println( ) and print( )
These methods are available through System.out.
System is a class predefined by Java
Java environment provides on several built-in class libraries that
contain many
built-in methods that provide support for such things as I/O, string
handling, networking,and graphics.
Java combination of the Java language plus its standard classes.

Bi-Gems Technologies
Data Types, Variables, and Arrays
The Primitive Types
java defines eight primitive data types
Integers This group includes byte, short, int, and long, which are for
whole-valued
signed numbers.
Floating-point numbers This group includes float and double, which
represent
numbers with fractional precision.
Characters This group includes char, which represents symbols in a
character set,
like letters and numbers.
Boolean This group includes boolean, which is a special type for
representing
true/false values.

Bi-Gems Technologies
Integers

Bi-Gems Technologies

Floating-Point Types

Bi-Gems Technologies
Variables
The variable is the basic unit of storage in a Java program. A variable is
defined by the
combination of an identifier, a type, and an optional initializer
Declare
int a, b, c; // declares three ints, a, b, and c.
int d = 3, e, f = 5; // declares three more ints, initializing
// d and f.
byte z = 22; // initializes z.
double pi = 3.14159; // declares an approximation of pi.
char x = 'x'; // the variable x has the value 'x'.

Bi-Gems Technologies
Dynamic Declaration

class DynInit {
public static void main(String args[]) {
double a = 3.0, b = 4.0;
// c is dynamically initialized
double c = Math.sqrt(a * a + b * b);
System.out.println("Hypotenuse is " + c);
}
}

Bi-Gems Technologies

a
class Scope {
public static void main(String args[]) {
int x; // known to all code within main
x = 10;
if(x == 10) { // start new scope
int y = 20; // known only to this block
// x and y both known here.
System.out.println("x and y: " + x + " " + y);
x = y * 2;
}
// y = 100; // Error! y not known here
// x is still known here.
System.out.println("x is " + x);
}
}

Bi-Gems Technologies

Life Time
class LifeTime {
public static void main(String args[]) {
int x;
for(x = 0; x < 3; x++) {
int y = -1; // y is initialized each time block is entered
System.out.println("y is: " + y); // this always prints -1
y = 100;
System.out.println("y is now: " + y);
}
}
}
The output generated by this program is shown here:
y is: -1
y is now: 100
y is: -1
y is now: 100
y is: -1
y is now: 100

Bi-Gems Technologies
The class is at the core of Java.
It is the logical construct upon which the entire Java language is built
because it defines the shape and nature of an object.
class forms the basis for object-oriented programming in Java.
Any concept you wish to implement in a Java program must be
encapsulated within a class
The data, or variables, defined within a class are called instance
variables.
The code is contained within methods.
Collectively, the methods and variables defined within a class are called
members of the class
Variables defined within a class are called instance variables because
each instance of
the class contains its own copy of these variables

Bi-Gems Technologies

Class demo

Bi-Gems Technologies

Class Demo

Bi-Gems Technologies

Class With Method

Bi-Gems Technologies

Method with return


voluem

Bi-Gems Technologies

Parametetarized method

Bi-Gems Technologies

Constructor

Bi-Gems Technologies

Parameter Constructor

Bi-Gems Technologies

Overloading Method

Bi-Gems Technologies

Over Loading
Constructor

Bi-Gems Technologies

Over Loading
Constructor

Bi-Gems Technologies

Object as parameter

Bi-Gems Technologies

Object as paramenter

Bi-Gems Technologies

Recursion

Bi-Gems Technologies

Understanding static
class member that will be used independently of any object of that
class.
class member must be accessed only in conjunction with an object
of its class.
With static it is possible to create a member that can be used by
itself, without reference to a specific instance
The most common example of a static member is main( ).

Bi-Gems Technologies

Nested class

Bi-Gems Technologies

Bi-Gems Technologies

Bi-Gems Technologies

Bi-Gems Technologies

Bi-Gems Technologies

Bi-Gems Technologies

Bi-Gems Technologies

Bi-Gems Technologies

Bi-Gems Technologies

You might also like