Java

48
JAVA AuD©

Transcript of Java

Page 1: Java

JAVAAuD©

Page 2: Java
Page 3: Java

JAVA

Page 4: Java

Java is a object oriented programming language which developed by James Gosling at Sun Microsystems (which is now a subsidiary of Oracle Corporation) and released in 1995 .

It is platform independent which can call as portable

Using java we can create interactive web pages or program to PC or mobile

So Lets get started…………………………………

Introduction to java

Page 5: Java

Over 3.5 people learn java and using it day by day in places like NASA , IBM and so.

Java was originally created for run simple web applications.

Introduction to java

Page 6: Java

Web servers Relational databases Orbiting telescopes PDA Cellular phones Blue-ray disks

What we can do??

Page 7: Java

James Gosling, Mike Sheridan, and Patrick Naught on initiated the Java language project in June 1991.Java was originally designed for interactive television, but it was too advanced for the digital cable television industry at the time. The language was initially called Oak after an oak tree that stood outside Gosling's office; it went by the name Green later, and was later renamed Java, from a list of random words. Gosling aimed to implement a virtual machine and a language that had a familiar C/C++ style of notation

History of java

Page 8: Java

Java applications Java application is a program that run from

command line interface Java Applets

Java applets are programs that run on web pages. It also compile on cmd but to run you must have web browser

Java servlet This is a special program that runs on web

browser(for on line soft ware)

Java programs

Page 9: Java

Simple Java program left many of the unnecessary

features of high level language Object oriented

Java programs use OOP concepts to create programs

Portable Java program can run in any flat form

(Windows, Linux, mac OS X etc…) as long as it install Java runtime(JVM)

Advantages of java

Page 10: Java

Multithreaded Java program can perform several tasks @

same time which most of programming languages can’t;

Secure High performance Distributed Dynamic

Advantages of java

Page 11: Java

How java work

Page 12: Java

What Is an Object? An object is a software bundle of related state

and behavior. Software objects are often used to model the real-world objects that you find in everyday life.

What is a class? Class is a collection of a objects with common

properties.

OOP

Page 13: Java

Software objects are conceptually similar to real-world objects: they too consist of state and related behavior. An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods (functions in some programming languages). Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication. Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation — a fundamental principle of object-oriented programming.

Object

Page 14: Java

Objects are key to understanding object-oriented technology. Look around right now and you'll find many examples of real-world objects: your dog, your desk, your television set, your bicycle. Real-world objects share two characteristics: They all have state and behavior. Dogs have state<attributes> (name, color, breed, hungry)

and behavior (barking, fetching, wagging tail). Bicycles also have state (current gear, current pedal

cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes). Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming.

Object

Page 15: Java

In the real world, you'll often find many individual objects all of the same kind. There may be thousands of other bicycles in existence, all of the same make and model. Each bicycle was built from the same set of blueprints and therefore contains the same components. In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles. A class is the blueprint from which individual objects are created.

Class

Page 16: Java

Different kinds of objects often have a certain amount in common with each other. Mountain bikes, road bikes, and tandem bikes,

for example, all share the characteristics of bicycles (current speed, current pedal cadence, current gear). Yet each also defines additional features that make them different: tandem bicycles have two seats and two sets of handlebars; road bikes have drop handlebars; some mountain bikes have an additional chain ring, giving them a lower gear ratio.

Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. In this example, Bicycle now becomes the superclass of MountainBike, RoadBike, and TandemBike. In the Java programming language, each class is allowed to have one direct superclass, and each superclass has the potential for an unlimited number of subclasses:

What Is Inheritance?

Page 17: Java

Inheritance

Page 18: Java

Interface makes the relationship between classes and functionality to those classes implement easier to understand and to design

A interface is a collection of methods that indicate a class has some behavior in addition to what in inherits from supper class;

Interface

Page 19: Java

Packages are use to grouping related classes and interfaces

Java has many packages than make our work lot easier

By default you have access to “java.lang” package;

For take advantages of other packages you must import them

Packages

Page 20: Java

Download java SE JDK http://www.oracle.com/technetwork/java/javase/

downloads/index.html

Install it Set Path All set & ready to write a program

Setup your computer

Page 21: Java

public class syntax{public static void main(String args[]){

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _}

}

Syntax

Page 22: Java

public class yourName{public static void main(String args[]){System.out.println(“IDM-Horana”);

}}

Pro1

Page 23: Java

A variable is a place where information can be stored while a program is running.

Kinds of variable Instance variable Local variable

Naming variable Name of variable must start with letter, “_”, “$” Can’t start with number Can’t use keyword to defining a variable

Variable

Page 24: Java

abstract continue for new switch

assert default goto package synchronized

boolean do if private thisbreak double implements protected throwbyte else import public throwscase enum instanceof return transient catch extends int short trychar final interface static voidclass finally long strictfp volatileconst float native super while

Keywords

Page 25: Java

<datatype> <variableName>;

EX:

public cass variabeEx1{public static void main(String args[]){

int x;x=10;int y=20;int a,b,c;

}}

Syntax

Page 26: Java

Data Type Default Value (for fields)

byte 0

short 0

int 0

long 0L

float 0.0f

double 0.0d

char '\u0000'

String (or any object) null

boolean false

Data types

Page 27: Java

public class varEx1{Public static void main(String args[]){int a,b;String c;a=10;b=15;c=“IDM-Horana”System.out.println(a);System.out.println(b);System.out.println(c);

}}

Examples

Page 28: Java

Arrays

Page 29: Java

public class arrayEx1 { public static void main (String args[]){ //Defining an array String[] array1; array1 = new String[5];

//add data to array array1[0]="Aaliyah"; array1[1]="Sera"; array1[2]="Emma"; array1[3]="Ely"; array1[4]="Wendy";

System.out.println(array1[0]); System.out.println(array1[1]); System.out.println(array1[2]); System.out.println(array1[3]); System.out.println(array1[4]); }}

Array example

Page 30: Java

Operator Precedence

Operators Precedence

postfix expr++ expr--

unary++expr --expr +expr -expr ~ !

multiplicative * / %

additive + -

shift << >> >>>

relational < > <= >= instanceof

equality == !=

bitwise AND &

bitwise exclusive OR ^

bitwise inclusive OR |

logical AND &&

logical OR ||

ternary ? :

assignment= += -= *= /= %= &= ^= |= <<= >>= >>>=

Operators

Page 31: Java

+ additive operator (also used for String concatenation) - subtraction operator

* multiplication operator / division operator % remainder operator

Arithmetic Operators

Page 32: Java

class ArithmeticEx1 { public static void main (String[] args){ int result = 1 + 2; // result is now 3 System.out.println(result); result = result - 1; // result is now 2 System.out.println(result); result = result * 2; // result is now 4 System.out.println(result); result = result / 2; // result is now 2 System.out.println(result); result = result + 8;System.out.println(result); // result is now 10 result = result % 7; System.out.println(result); // result is now 3 }}

Arithmetic Operators Example

Page 33: Java

+ Unary plus operator; indicates positive value (numbers are positive without this, however)

- Unary minus operator; negates an expression

++ Increment operator; increments a value by 1

-- Decrement operator; decrements a value by 1

! Logical complement operator; inverts the value of a boolean

Unary Operators

Page 34: Java

class UnaryEx1 { public static void main(String[] args){ int result = +1; // result is now 1 System.out.println(result); result--; result is now 0 System.out.println(result); result++; // result is now 1 System.out.println(result); result = -result; // result is now -1 System.out.println(result); boolean success = false; System.out.println(success); // false System.out.println(!success); // true } }

Unary Operators Example

Page 35: Java

== equal to != not equal to > greater than >= greater than or equal to < less than <= less than or equal to

Comparison Operators

Page 36: Java

class ComparisonDemo { public static void main(String[] args){ int value1 = 1; int value2 = 2if(value1 == value2) System.out.println("value1 == value2"); if(value1 != value2) System.out.println("value1 != value2"); if(value1 > value2) System.out.println("value1 > value2"); if(value1 < value2) System.out.println("value1 < value2"); if(value1 <= value2) System.out.println("value1 <= value2"); }

Comparison Operators Example

Page 37: Java

IF else Switch While Do while For

Flow Control

Page 38: Java

IF else

Page 39: Java

import java.util.*;

class ifElseEx1{

public static void main(String args[]){int x;Scanner input=new Scanner(System.in);System.out.print("Enter number for X:-");x=input.nextInt();if(x<=10){System.out.println("X is less than 10");}else{System.out.println("X is Grater than 10");}}}

If else Example

Page 40: Java

import java.util.*;class ifElseEx2 { public static void main(String args[]){ //declaring variables int m1,m2,m3,tot; double avg; //inputs Scanner mark = new Scanner(System.in); System.out.print("Enert maek 1:-"); m1=mark.nextInt(); System.out.print("Enert maek 2:-"); m2=mark.nextInt(); System.out.print("Enert maek 3:-"); m3=mark.nextInt(); //calculations tot = m1+m2+m3; avg = tot/3; System.out.println("Total mark is:-"+tot); System.out.println("Average mark is:-"+avg); //Selecton if(tot>=210){ System.out.println("You are Selected"); }else{ System.out.println("Try again"); } }}

More If else

Page 41: Java

import java.util.*; class switchEx1 {

public static void main(String args[]){ int x; Scanner num=new Scanner(System.in); System.out.print("Enter root number:-"); x=num.nextInt();

switch(x){ case 281:System.out.println("Thalgahawila-Horana"); break; case 282:System.out.println("Padukka-Horana"); break; case 315:System.out.println("Meepe-Horana"); break; case 120:System.out.println("Colombo-Horana"); break; case 450:System.out.println("Panadura-Horana"); break; default:System.out.println("Enter valid root no"); } } }

Switch

Page 42: Java

While

Page 43: Java

public class flowWhile {

public static void main(String []args){ int a = 0; while(a<=10){ System.out.println(a+"> AuD"); a++; } System.out.println("Done"); }

}

While Example

Page 44: Java

Do While

Page 45: Java

public class flowDoWhile {

public static void main (String[] args){ int x=0; do{ System.out.println(x+"> AuD"); x++; } while(x<=10); }}

Do while Example

Page 46: Java

For

Page 47: Java

public class flowFor { public static void main(String args[]){ for(int a=0;a<=10;a++){ System.out.println(a+"> Aud"); } System.out.println("Done........"); }}

For Example

Page 48: Java

Thank YouAuD©