Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (1 vote)
7K views

Computer Java Introduction For Class 9 Icse

The document provides an introduction to the Java programming language. It discusses the history and types of Java applications, features of Java like being object-oriented and platform independent. It also covers Java concepts like bytecode, JVM, OOP principles, packages, keywords, comments, Java program structure, tokens, data types, operators and type conversion.

Uploaded by

rajassinha123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
7K views

Computer Java Introduction For Class 9 Icse

The document provides an introduction to the Java programming language. It discusses the history and types of Java applications, features of Java like being object-oriented and platform independent. It also covers Java concepts like bytecode, JVM, OOP principles, packages, keywords, comments, Java program structure, tokens, data types, operators and type conversion.

Uploaded by

rajassinha123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Introduction to Java

Java:- Java is a high level , robust, object oriented and secure programming language. Java was
developed by James Gosling and his colleagues at Sun Micro System in 1991.
Types of Java Applications:-
1. Standalone Application:- Program needs to install on every computer.
2. Java applets(Web application):- Runs on server side and can be used with the help of
a web browser.
3. Enterprise Application:- Runs on server and distributed and used in a network. eg.
Banking application.
4. Mobile Application: - Application developed for mobile devices.

Features of Java:-
1. It is object-oriented programming language.
2. It is case sensitive language.
3. It is two stage programming language i.e. compiled as well as interpreted.
4. It is platform independent programming language.

Source Code--------------------→Byte Code------------------→Machine Code


compiler interpreter
Source Code:- Program written by the user.
Byte Code:- Intermediate code obtained after the compilation of source code. It is platform
independent code.
Java Virtual Machine(JVM):-Java Interpreter which converts ‘Byte Code’ into machine
code.
Machine Code:- Machine code is the code obtained after interpreting byte code. This code is
understood by the computer.

Principles of Object Oriented Programming Language: -


1. Data Abstraction: It is an act of representing the essential features without showing the
background details.
2. Inheritance: It is the mechanism in Java by which one class is allowed to inherit the
features(fields and methods) of another class. In Java, Inheritance means creating new classes
based on existing ones.
3. Polymorphism: The word polymorphism means having many forms. In simple words, we
can define Java Polymorphism as the ability of a method/function to be used in more than one
form.
4. Encapsulation: It refers to the bundling of data and methods that operate on that data
within a single unit, which is called a class in Java.

Java Package:- Java package is a collection of various classes. Each class contains different
functions. A package can be included in a program by using a keyword ‘import’.
eg. import java.io.*;
import java.util.*;
Some other packages are, java.lang, java.net, java.applet, java.awt, java.txt, java.math etc.

Keyword:- Keyword is a reserved word which is used for some specific purpose in the
programs. eg. class, if, System, break, else
Comment Lines:- Comment lines are non-executable lines written anywhere in the program
for describing the program or code.
It is of three types:-
I. Single Line Comment:- // used to write a comment in one line
II. Multiline Comment:- /* Used to write
comments in multiple lines */
III. Documenting Comment:- /** used to write
comments in paragraphs */

Structure of a Java Program


// My first Java Program <------ Comment
public class Example
{
public static void main (String args[] )
{
int a=10,b=20,c=0;
c=a+b;
System.out.println(“Sum=” +c);
}
}

TOKEN:-
It is the smallest individual unit of a program.
eg. variable, constant, operator etc.
1. Identifier/ Variable
2. Assignment
3. Literal (Constant)
4. Punctuator ; . ?
5. Separator , () {} []
6. Operator
7. keyword

1.Identifier/Variable:- It is a token whose value keeps on changing according to the user’s


requirement.
eg. int a;
a=5;
Rules for naming a variable:-
a) It must start with an alphabet.
b) It can contain a digit but only at the end.
c) Any keyword can not be a variable name.
d) No special symbol except underscore (_) and dollar($) sign can be used.
2.Assignment:- Assignment token is used to store a constant in a variable.
eg. a=10;
3.Literals:- Literals are the constants which do not change throughout the program.
a)Integer Literal:- Any number either negative or positive without a decimal point is
called an integer literal.
eg. 10, -2, 5234, -540 etc.
b)Real Literal:-Any number either negative or positive with a decimal point is known as
real or floating point literal.
eg. -2.5, 0.304, 65.20 etc.
c)Character Literal:- Any digit, alphabet, special symbol enclosed within single quotes.
eg. ‘A’ , ‘7’, ‘*’ , ‘p’
d)String Literal:- A group of alphanumeric characters enclosed within double quotes is called a
string literal.
eg. “Computer” , “10+2” , “ABC123”

e)Punctuators:- These are punctuation signs used as special characters.


eg. ? . ;
f)Separators:- Separators are special characters used to separate the variables or characters.
eg. , () {} []
g)Operators:- Symbols or tokens that perform arithmetical or logical operations.
eg. +, -, *, /, <, > etc.

Data Types in Java


A datatype restricts the values that a variable or a function might take. It also defines how the
values of that data type are stored in memory.
Data Type

Primitive types Non Primitive types

Numeric Types Non Numeric Types Class Array Interface

Integer Type Floating Type boolean char


i)byte i)float String
ii)short ii)double
iii)int
iv)long

1)Primitive types:- These are basic data types which are independent of any other data type.
byte-----→1 byte(8 bits) ------------→ -128 to 127
short----→2 bytes(16 bits)---------→ -32768 to 32767
int ------→4 bytes(32 bits)----------→ -231 to 231-1
long----→8 bytes(64 bits)----------→ -263 to 263-1
float----→4 bytes(32 bits)--------→ upto 7 digits after decimal
double--→8 bytes(64 bits)-------→ upto 15 digits after decimal
char----→ 2 bytes(16 bits)-------→ Single Character
String--→ More than 16 bits-----→ Set of characters

ASCII CODES:-
A-Z (65-90)
a-z (97-122)
0-9 (48-57)

a= b + c * 4 - 10
Arithmetic Expression

Arithmetic Statement

Pure Expression:-An arithmetic expression that uses all its components of same data types.
Impure Expression(Mixed Expression):- An arithmetic expression in which one or more
components are of different data types.
[ Type Conversion/ Type casting]
Conversion of one data type into another.
1) Implicit type conversion: - In this conversion the data types are converted from lower data
type to higher data type automatically.
byte
char
short
int
long
float
double
(Hierarchy of Data Types- Lower to Higher)

example:- int a; long b; long c;


c = a + b;
long= int + long
2) Explicit type conversion:- In this conversion the data types are converted from one type
to another type by the user, either from lower to higher or higher to lower.
example 1:- int a,b;
float c = (float) (a+b);
example 2:- double x,y;
int c= (int) (x+y);

[Operators]

A symbol or token which performs arithmetical, relational or logical operations.


Arithmetical Operator Relational Operator Logical Operator
< &&
> ||
>= !
Unary Binary Ternary <=
unary + +,- ? : ==
unary - *,/ !=
unary increment (++)
unary decrement ( --)

a) Unary Operator:- This operator is used to operate on single operand.


( unary + ) used as pointer to a variable.
eg. a = 8 then +a is 8
a = -8 then +a is -8
( unary - ) used in the same way as unary + but it reverses the sign of the operand.
eg. a = 8 then +a is -8
a = -8 then -a is 8
( unary increment ++) increases the value of an operand by one.
• (++a) prefix / preincrement :- It increments the value by one before use.
eg.1) a =5
b = ++a + 10;
= 6 + 10 = 16
eg.2) b= ++a + 10 + a;
= 6 + 10 +6 =22
• (a++) postfix / postincrement :- It increments the value by one after use.
eg. 1) a = 5
b = a++ + 5;
= 5 + 5 =10
eg. 2) b = a++ + 5 + a++;
= 5 + 5 + 6 = 16
eg. 3) b = a++ + 5 + ++a;
= 5 + 5 + 7 = 17
( unary decrement --) decreases the value of an operand by one.
• (- -a) predecrement :- It decrements the value by one before use.
eg. 1) a = 5
b = --a + 10
= 4 + 10 = 14
eg 2) b = --a +10 + a
= 4 + 10 + 4 = 18
• (a- -) postdecrement :- It decrements the value by one after use.
eg. 1) a = 5
b = a- - + 10;
5 + 10 = 15
eg 2) b = a - - + 10 + a;
Operator Type Associativity

() Parentheses Left to Right


[] Array subscript
· Member selection

++ Unary post-increment Right to left


-- Unary post-decrement

++ Unary pre-increment Right to left


-- Unary pre-decrement
+ Unary plus
- Unary minus
! Unary logical negation
~ Unary bitwise complement
(type) Unary type cast

* Multiplication Left to right


/ Division
% Modulus

+ Addition Left to right


- Subtraction

< Relational less than Left to right


<= Relational less than or equal
> Relational greater than
>= Relational greater than or equal

== Relational is equal to Left to right


!= Relational is not equal to

&& Logical AND Left to right

|| Logical OR Left to right

?: Ternary conditional Right to left

= Assignment Right to left


+= Addition assignment
-= Subtraction assignment
*= Multiplication assignment
/= Division assignment
%= Modulus assignment
= 5 + 10 + 4 = 19
Hierarchy of Operators: -
** HIGHER TO LOWER PRECEDENCE**

eg. ) int a=5, b=7, c=10, d;


i) d = a++ % b-- + b * c;
= 5 % 7 + 6 * 10;
= 5 + 60 = 65
ii) d = --a / --b + c * 10;
= 4 / 6 + 10 * 10
= 0 + 100 = 100
iii) d = --c % a-- + a * 2;
=9%5+4*2
= 4 + 8 = 12

Shorthand Operations:
a=a+b => a += b
a=a–b => a -= b
a=a*b => a *= b
a=a/b => a /= b
a=a%b => a %= b

eg.) if a = 8
a + = ++a + a++ +4;
a= a + ( ++a + a++ +4)
= 8 +(9 + 9 +4)
= 8 + 22 = 30
b) Binary Operators:- An arithmetic operator which deals with two operands.
+ addition
-subtraction
* multiplication
/ division
% modulus(remainder)
c) Ternary Operator:- This is a conditional operator which deals with three operands.
variable = (condition) ? expression1 : expression2

true

false

eg.) int a= 5, b= 6;
int c = ( a> b ) ? a : b ;
output c = 6;
d) Relational Operators:- An operator which compare two operands and determine the
relationship between them. It returns either a true value or a false value.
< less than
> greater than
<= less than or equal to
>= greater than or equal to
= = equal to
! = not equal to
eg.) a = 5, b = 7;
i) a > b (false)
ii) a + b = = 12 (true)
e) Logical Operators:- These operators return true or false depending upon the outcome of
different conditions.
AND (&&) (a>b) && (a>c)
OR ( | | ) (a>b) | | (a>c)
NOT ( ! ) ! (a==b)
1.) AND(&&)
Condition1 Condition2 Result
True False False
False True False
False False False
True True True
2.) OR( | | )
Condition1 Condition2 Result
True False True
False True True
False False False
True True True
3.) NOT ( ! )
Condition Result
True False
False True

eg.) 5 > 3 && 3 < 5 True


6 = = 6 && 3>0 True
8>4 && 9>=12 False
5 >4 || 8>12 True
2 < 0 || 10>15 False
!(8>3) False
!(3<0) True
Java Programming(Input in Java)
1.) Static Input:- When we store the value in a variable in the program itself then it is known
as static input.
eg. int a=5;
int b=10;
int c=a+b; => c=15;
2.) Dynamic Input:- When we input a value in a variable while executing the program then
it is known as dynamic input method.
Dynamic Input

Input by function argument Input by InputStreamReader Input by Scanner


class class
a) Input by function argument:- The values of the variables are provided as argument of the
main() function.
Eg. public static void main(int a, int b)
b) Input by InputStreamReader Class:- The values of the variables are provided with the help
of combination of two classes, ‘InputStreamReader’ and ‘BufferedReader’. java.io package
must be included before using these two classes.
c) Input by Scanner Class:- The Scanner Class allows us to read values of various types while
executing the program( run time). Scanner class is available under java.util package, it must
be imported to use the Scanner class.
Process to import the Scanner class in your program.
1. Use import keyword
eg. import java.util.*; or import.java.util.Scanner;

2. Create Scanner Object


Scanner sc = new Scanner(System.in);

Scanner functions to take inputs:-


Scanner ob = new Scanner(System.in);
1.) nextShort();
takes short data type value.
eg. short a = ob.nextShort();
2.) nextInt();
takes int data type value.
eg. int a=ob.nextInt();
3.) nextFloat();
takes float data type value.
eg. float a = ob.nextFloat();
4.) nextDouble();
takes double data type value.
eg. double a = ob.nextDouble();
5.) nextLong();
takes long data type value.
eg. long a = ob.nextLong();
6.) next();
takes set of characters until whitespace comes.(that is a single word)
eg. String a = ob.next();
7.) nextLine();
takes the entire line as a String.
eg. String a = ob.nextLine();
8.) next().charAt();
extracts a character from a String at given index/position.
eg. char a= ob.next().charAt(0); will extract the character stored at 0th position.
Program 1. WAP to enter two numbers and print the sum.

import java.util.*;
public class Sum
{
public static void main()
{
Scanner sc = new Scanner(System.in);
int a,b,c;
System.out.println(“Enter two numbers”);
a= sc.nextInt();
b=sc.nextInt();
c=a+b;
System.out.println(“Sum=” +c);
}
}
Mathematical Library Methods/Functions
The methods which are created by the system developers are called library methods or built-in
methods. Mathematical methods are used to carry out mathematical operations. All
mathematical methods are included in ‘Math’ class of java.lang package.
1.) Math.min()
This function returns the minimum of two numbers. Return type depends upon the arguments of
the function.
eg. int n=Math.min(12,22); then n=12
double n=Math.min(4.5, 2.5); then n= 2.5
2.) Math.max()
This function returns the maximum of two numbers. Return type depends upon the arguments of
the function.
eg. int n=Math.max(12,22); then n=22
double n=Math.max(4.5, 2.5); then n= 4.5
3.) Math.pow()
This function returns the value of first argument raised to the power to second argument. Return
type is always double data type.
eg. double n=Math.pow(2,3); then n= 8.0
4.) Math.sqrt()
This function returns the square root of a positive number. It will return a double data type value.
eg. double n= Math.sqrt(9.0); then n= 3.0
Note- Square root of a negative number is imaginary value.
5.) Math.cbrt()
This function returns the cube root of a positive or a negative number. It will return a double
data type value.
eg. double n= Math.cbrt(27.0); then n=3.0
double n= Math.cbrt(-27.0); then n= -3.0
6.) Math.abs()
This function returns the absolute value of an argument (i.e. a positive number). Return type
depends upon the arguments of the function.
eg. int n= Math.abs(8) then n=8
double n= Math.abs( -8.0) then n=8.0
7.) Math.log()
This function returns the natural logarithmic value of a given argument in double data type.
eg. double n= Math.log(6.25); then n=1.8325
8.) Math.random()
This function returns a random number between 0 and 1. It will return a double data type value.
eg. double n= Math.random();
It will return any fractional number between 0 and 1.
9.) Math.round()
This function returns the value in rounded off form. It will return a value either in int data type
or long data type.
Rules for positive numbers: -
i) fractional part lies between 0(inclusive) and 0.5(exclusive)
Then, integer part of same number.
eg. Math.round(8.0) => 8
Math.round(8.49) => 8
ii) fractional part is more than or equal to 0.5
Then , next higher integer number
eg. Math.round(8.5) => 9
Math.round(8.99) => 9
Rules for negative numbers: -
i) fractional part lies between 0(inclusive) and 0.5(inclusive)
Then, integer part of same number.
eg. Math.round( -8.0) => -8
Math.round( -8.5) => -8
ii) fractional part is more than 0.5
Then , next lower integer number
eg. Math.round( -8.51) => -9
Math.round( -8.99) => -9
10.) Math.rint()
This function returns the nearest integer value for a given argument. It will always return a
double data type value.
Rules for positive numbers: -
i) fractional part lies between 0(inclusive) and 0.5(inclusive)
Then, integer part of same number.
eg. Math.rint(8.0) => 8.0
Math.rint(8.5) => 8.0
Math.rint(7.5) =>8.0 (if odd integer then next higher even number)
ii) fractional part is more than or equal to 0.5
Then , next higher integer number
eg. Math.rint(8.51) => 9.0
Math.rint(8.99) => 9.0
Rules for negative numbers: -
i) fractional part lies between 0(inclusive) and 0.5(inclusive)
Then, integer part of same number.
eg. Math.rint( -8.0) => -8.0
Math.rint( -8.5) => -8.0
Math.rint(-9.5) => -10.0 (if odd integer then next lower integer)
ii) fractional part is more than 0.5
Then , next lower integer number
eg. Math.rint( -8.51) => -9.0
Math.rint( -8.99) => -9.0
11.) Math.ceil()
This function returns the higher integer value for a given argument. It will always return a
double data type value.
eg. For positive numbers:
Math.ceil(8) => 8.0
Math.ceil(8.1) => 9.0
Math.ceil(8.95) => 9.0
eg. For negative numbers:
Math.ceil(-0.41) =>-0.0
Math.ceil(-8) => -8.0
Math.ceil(-8.1) => -8.0
Math.ceil(-8.95) => -8.0
12.) Math.floor()
This function returns the lower integer value for a given argument. It will always return a double
data type value.
eg. For positive numbers:
Math.floor(8) => 8.0
Math.floor(8.1) => 8.0
Math.floor(8.95) => 8.0
eg. For negative numbers:
Math.floor(-0.41) =>-1.0
Math.floor(-8) => -8.0
Math.floor(-8.1) => -9.0
Math.floor(-8.95) => -9.0

You might also like