Topic 0: Java SE 7

41
Topic 0 : Java SE 7 DDOOCP

description

Slides

Transcript of Topic 0: Java SE 7

Page 1: Topic 0: Java SE 7

Topic 0 : Java SE 7DDOOCP

Page 2: Topic 0: Java SE 7

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.

Page 3: Topic 0: Java SE 7

Features of Java

1. Portability

2. Memory Management

3. Security

4. Distributed Access

5. Multithreading

Page 4: Topic 0: Java SE 7

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

Page 5: Topic 0: Java SE 7

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

Page 6: Topic 0: Java SE 7

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

Page 7: Topic 0: Java SE 7

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

Page 8: Topic 0: Java SE 7

Compilation of Java Code

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

Page 9: Topic 0: Java SE 7

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

Page 10: Topic 0: Java SE 7

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

Page 11: Topic 0: Java SE 7

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

Page 12: Topic 0: Java SE 7

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.

Page 13: Topic 0: Java SE 7

3. Separator

Name of the Separator Symbol

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

Page 14: Topic 0: Java SE 7

4. Operator

› We will discuss in coming slide.

Page 15: Topic 0: Java SE 7

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

Page 16: Topic 0: Java SE 7

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. */

Page 17: Topic 0: Java SE 7

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

Page 18: Topic 0: Java SE 7

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;

Page 19: Topic 0: Java SE 7

Operators

› String Operator

› Arithmetic operators

– Binary Operators

– Unary Operators

› Assignment operators

› Comparison operators

› Logical operators

› Conditional Operators

Page 20: Topic 0: Java SE 7

1. String Operator +

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

Page 21: Topic 0: Java SE 7

2. Arithmetic Operators

Opertor Meaning

+

-

*

/

% Modulus

› Binary Operator– Needs two operands

Page 22: Topic 0: Java SE 7

2. Arithmetic Operators

› Unary Operator– Needs only one operand

++ -- -

Page 23: Topic 0: Java SE 7

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

Page 24: Topic 0: Java SE 7

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

Page 25: Topic 0: Java SE 7

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

Page 26: Topic 0: Java SE 7

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

Page 27: Topic 0: Java SE 7

Operator Precedence

› Increment and decrement operations

› Arithmetic operators

› Comparison operators

› Logical operators

› Assignment operators

Page 28: Topic 0: Java SE 7

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

Page 29: Topic 0: Java SE 7

if else statement

Page 30: Topic 0: Java SE 7

switch Statement

Page 31: Topic 0: Java SE 7

switch Statement

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

Page 32: Topic 0: Java SE 7

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.

Page 33: Topic 0: Java SE 7

while loop› Display odd number from 1 to 100.

Page 34: Topic 0: Java SE 7

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.

Page 35: Topic 0: Java SE 7

Nested Loop

Page 36: Topic 0: Java SE 7

break Statement

› WAP to display first five odd numbers.

Page 37: Topic 0: Java SE 7

continue Statement

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

Page 38: Topic 0: Java SE 7

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

Page 39: Topic 0: Java SE 7

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.

Page 40: Topic 0: Java SE 7

Two-Dimensional Array

Page 41: Topic 0: Java SE 7

Next Class…

1. What is a class?

2. What is an object?

3. Constructor

4. Method Overloading

5. Packages

6. Inner Class