You are on page 1of 27

Java

Overview

Accent e Technology pvt. Ltd.


http://www.accentonline.co.in

Programming Languages
Programming Language. Reasons for development of a new programming language.
To adapt to the changing environment and its uses. To implement refinement and improvement in the art of programming.

Previous Programming Languages.

Modern Programming - C
C was developed by Dennis Ritchie in the year 1972 for Bell Laboratories. Structured Programming Concept. Top to Bottom Execution. Problems arise if the program is huge. For example, the program exceeding 25,000 lines.

C with Classes C++


Developed by Bjarne Stroustrup in the year 1979 for Bell Laboratories. Enhancement of C with Object Oriented Programming Concepts. Later in 1983, it was named as C++. Bottom to Top Execution. Problems started with Platform Dependency.

Java
Idea conceived by James Gosling, Ed Frank, Chris Warth, Mike Sheridan and Patrick Naughton in 1991. First working version was released in 1992 as Oak. In 1995, it was renamed as Java and released for public use. It was developed by Sun Microsystems. Pure Object Oriented Programming Concepts.

Features of Java
Simple Secure Portable Architecture - Neutral Interpreted Multithreaded Robust Distributed Dynamic Object-oriented

High performance

Java Compilation & Execution


Command Line Interface

Programmer

Editor Source File(s) (.java) Compiler (javac) Class File(s) (.class) Virtual Machine (java) Program executes

Parts of Sun Java SDK

Object-Oriented Concepts
The Essential Element
Abstraction

The Three OOP Principles


Encapsulation Inheritance Polymorphism

Variables
A variable is a storage location and has an associated type. Before you use a variable, you must declare it. Variable declaration
Format: Example: dataType variableName; int age;

Every variable has a name, a type and a value. Declaring a variable, reserves memory space.

Datatypes
Every variable must have two things: a data type and a name (identifier). Data Types defines the kind of data the variable can hold. There are two kinds of types in the Java programming language:
Primitive Type Reference Type

Primitive Types
A primitive type is predefined by the Java programming language and named by its reserved keyword.
- byte - short - int - long - float - double - char - boolean

Reference Types
There are three kinds of reference types:
class interface array

These are user defined values. They differ depending upon the user declarations.

Introduction to Arrays
An array is an object used to store a collection of data. All the data stored in the array must be of the same type. Declaration :
- int[ ] a = new int[3]; a[0]=1, a[1]=2, a[2]=3; - int[ ] a = {1,3,5,6,9};

Arithmetic Operators
+ * / % ++ -to indicate addition to indicate subtraction to indicate multiplication to indicate division to indicate remainder of a division (integers only) increment (prefix and postfix) decrement (prefix and postfix)

Relational Operators
== != < > <= >= equals not equals less than greater than less than or equal to greater than or equal to

Note: Arithmetic comparisons result in a Boolean value of true or false

Bitwise Logical Operators


The bitwise logical operators are

& | ^ ~

The Bitwise AND The Bitwise OR The Bitwise XOR The Bitwise NOT

Short-Circuit Logical Operators


Short-Circuit Logical Operators are Boolean operators, not found in most other computer languages. These are secondary versions of the Boolean AND and OR operators.
 &&  || Short-circuit AND Short-circuit OR

Assignment and Ternary Operators


Assignment Operator
Assignment operator has this general form: var = expression;

Ternary Operator
The ? has this general form:
expression1 ? expression2 : expression3 expression1 can be any expression that evaluates to a boolean value. If expression1 is true, then expression2 is evaluated; otherwise, expression3 is evaluated.

Control Statements
There are three types of Control Statements in Java.
Iteration Statements
for , while , dowhile.

Selection Statements
if , elseif , switch.

Branching Statements
break , continue , return.

while loop
while ( condition ) { statement1; statement2; ... statementN; } If the body has only one statement, the braces are optional condition is any logical expression, as in if The body of the loop

while ( condition ) statement1;

dowhile loop
do { statement1; statement2; ... statementN; } while ( condition ); Always use braces for readability (even if the body has only one statement) The code runs through the body of the loop at least once if condition is false, the next iteration is not executed

for loop
Initialization for ( initialization; condition; change ) { statement1; statement2; ... statementN; } Testing Change

if statement
Syntax if (Expression) { Action; } If the Expression is true then execute Action Action is either a single statement or a group of statements (in block) within braces

Expression

true

false

Action

if.else statement
Syntax if (Expression) { Action1 ; } else { Action2 ; } If Expression is true then execute Action1 otherwise execute Action2.

Expression true Action1 false

Action2

Multibranch if.else statement


Syntax: if(Boolean_Expression_1) { Action_1 ; } else if(Boolean_Expression_2) { Action_2 ; } else if(Boolean_Expression_3) { Action_3 ; } . else { Default_Action ; }

switch case statement


Syntax: switch( Controlling_Expression ) { Only byte , int , char , case Case_label_1: short are allowed { Statement; break; } case Case_label_N: { Statement; break; } Default: { Statement;. Break; } }

Branching statements
The Break-Statement
- The break Statement can be used in a switchstatement or in any kind of loop statement. When the break-statement is executed, the immediately enclosing loop (or Switch-statement) ends, and the remainder of the loop body is not executed.

The Continue-Statement
- The continue statement proceeds with the next iteration (repetition) of the immediately enclosing while, for or do/while structure.

You might also like