Topic 0: Java SE 7

Post on 18-Jan-2015

609 views 0 download

Tags:

description

Slides

Transcript of Topic 0: Java SE 7

Topic 0 : Java SE 7DDOOCP

Java

› Object-oriented programming language inherited from C++.

› Java is strong typed programming language.

› Developed by James Gosling, a software developer at Sun Microsystems in 1991

› Previously known as Oak

› Later renamed as Java in 1995 and released for public use.

› Java SE 7 is the first release of Java SEunder the Oracle Corporation.

Features of Java

1. Portability

2. Memory Management

3. Security

4. Distributed Access

5. Multithreading

Types of Java Programs

1. Desktop Applicationa. Character User Interface ( CUI )

A console based application

b. Graphical User Interface ( GUI )

2. Web Applicationa. Applets

b. Servlets

Java Development Kit (JDK)

› The development tools for Java provided as a part of the system.

› JDK comprises of the following development tools :

1. java

2. javac

3. javacdoc

4. jar

Java Platform Components

1. The Java Platform, Standard Edition (Java SE)– Most commonly used platform

– Used to develop desktop and console – based applications

2. The Java Platform, Enterprise Edition (Java EE)– Used to build server side applications

3. The Java Platform, Micro Edition (Java ME)– Used to build Java applications for micro-devices such as mobile phones

JVM or Java Runtime Environment (JRE )

› Java Virtual Machine

› In other programming language, a compiler translates source code into machine code for a specific computer.

› But in Java, a compiler translates the Java source code into an intermediate code known as bytecode.

› Bytecode is the machine language for JVM and is not machine specific.

› The machine specific code is generated by the Java interpreter which serves a mediator between a virtual machine and a real machine

Compilation of Java Code

› Process of compilation of Java code and conversion of bytecode into machine code

Java API

› Applications Program Interfaces (API)

1. java.awt

2. java.io

3. java.lang

4. java.math

5. java.net

6. java.sql

7. javax.swing

8. java.util

Tokens in Java

› Tokens are the smallest units or building blocks used for creating programs.

› Six types of tokens in Java

1. Keyword

2. Identifier

3. Separator

4. Operator

5. Literal

6. Comment

1. Keyword

› Predefined words that have specific meanings in Java.

› Keywords cannot be used as names for variables, methods, or classes.

The Java Keywords

if for while do

imports implements extends double

float else return class

interface void package new

private public protected default

switch super boolean char

2. Identifier

› An identifier is a name given to a variable, method or class.

› Rules for naming an identifier

1. It must start with an underscore, a letter or a dollar sign ($).

2. It may consist of a letter, a number, an underscore or a dollar sign.

3. Spaces are not allowed in between an identifier.

4. Identifiers are case sensitive.

5. Java keywords should not be used while naming an identifier.

3. Separator

Name of the Separator Symbol

Parentheses ( )Braces { }Brackets [ ]Semicolon ;Comma ,Period .

4. Operator

› We will discuss in coming slide.

5. Literal

› Value assigned to variable or constant.

– 20 an integer literal

– 12.344 a double literal

– true a Boolean literal

– ‘a’ a character literal

– “Java” a string literal

6. Comment

› Comments are the tokens that are ignored by compiler during the compilation of the program.

› Used to describe the function of a particular code.

› Two types of comment in Java1. Single Line Comment

› // This is a single line comment.

2. Multiple Line Comment

› /* This is a multiple line comment. */

Data types

› You cannot declare a variable without specifying its data type.

› The type of data that can be stored in a variable.

› 4 categories1. Integer

› byte, short, int, long

2. Character

› char

3. Floating Point

› float, double

4. Boolean

› boolean

Variables

› Containers or placeholders to store data within a program.

› The value of variable can be changed through out the program.

› Syntax :

– dataType identifier; //declaring variable

› E.g.

– int a;

– float b, c, d;

– char x;

– int a = 20, b = 40;

– boolean x;

Operators

› String Operator

› Arithmetic operators

– Binary Operators

– Unary Operators

› Assignment operators

› Comparison operators

› Logical operators

› Conditional Operators

1. String Operator +

› It is used to concate or join two or more strings.

2. Arithmetic Operators

Opertor Meaning

+

-

*

/

% Modulus

› Binary Operator– Needs two operands

2. Arithmetic Operators

› Unary Operator– Needs only one operand

++ -- -

3. Assignment Operators

Opertor Example Is same as Meaning

= a = b a = b Assignment

+= a += b a = a + b Addition Assignment

*= a *= b a = a * b Multiplication Assignment

-= a -= b a = a – b Subtraction Assignment

/= a /= b a = a / b Division Assignment

%= a %= b a = a % b Modulus Assignment

4. Comparison Operators

› Compares two values and returns a Boolean value.

Operator Example

== equal to

!= not equal to

< less than

> greater than

>= greater than or equal to

<= less than or equal to

5. Logical Operators

› They operates only on the boolean operands and produce the boolean result.

› They are used in compound condition.

› Write the truth table of OR, AND operators for 3 inputs.

› Write the truth table of NOT operator.

Operator Meaning

|| OR

&& AND

! NOT

6. Conditional Operator

› It assigns value to a variable based on the specific condition.

› It is a ternary operator. It requires 3 operands.

› Syntax :

(condition)?expression1:expression2

Operator Precedence

› Increment and decrement operations

› Arithmetic operators

› Comparison operators

› Logical operators

› Assignment operators

Control Flow Statements

1. Decision Making Statements

a. if else statement

b. switch statement

2. Looping Statements

a. for loop

b. while loop

c. do while loop

3. Jump Statements

a. break

b. continue

if else statement

switch Statement

switch Statement

› Write a program to find whether a character is vowel or not.

for loop

› Display 1 to 10;

› Display even number from 1 to 100.

› WAP to find the factorial of the given number.

› Find whether a given number is prime or not.

while loop› Display odd number from 1 to 100.

do while loop

› WAP to take input from user until user enters 0.

› WAP to take input from user until user enters "Bye“.

› WAP to sum until user enter 0. Display the sum.

Nested Loop

break Statement

› WAP to display first five odd numbers.

continue Statement

› WAP to display from 1 to 100. But don’t display multiples of 5.

Array

› An array is a collection of similar type of data.

› It helps to store multiple data under same name.

› Its index always starts from 0.

› Two categories

– One-dimensional array

– Two-dimensional array

One-Dimensional Array

› WAP to declare array of full name and age. The size of array must be taken from user. Take the full name and age and display them.

Two-Dimensional Array

Next Class…

1. What is a class?

2. What is an object?

3. Constructor

4. Method Overloading

5. Packages

6. Inner Class