0% found this document useful (0 votes)
5 views16 pages

Java Unit-1

The document provides an overview of Java, a high-level, object-oriented programming language developed by Sun Microsystems, highlighting its features such as platform independence, robustness, and security. It also compares Java with C++, discusses the Java Virtual Machine (JVM), and outlines the Java Development Kit (JDK) and Application Programming Interface (API). Additionally, it covers Java programming structure, data types, and tokens, along with examples and explanations of basic Java concepts.

Uploaded by

bkcda432
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views16 pages

Java Unit-1

The document provides an overview of Java, a high-level, object-oriented programming language developed by Sun Microsystems, highlighting its features such as platform independence, robustness, and security. It also compares Java with C++, discusses the Java Virtual Machine (JVM), and outlines the Java Development Kit (JDK) and Application Programming Interface (API). Additionally, it covers Java programming structure, data types, and tokens, along with examples and explanations of basic Java concepts.

Uploaded by

bkcda432
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

BSC –II Semester-IV

[Link]. II Semester IV
Paper - I
UNIT - 1
1.1 JAVA
Java is a high-level, third generation programming language, like C, FORTRAN, Smalltalk, Perl etc.
Java is a general-purpose, object-oriented programming language developed by Sun Microsystems of
USA in [Link] name was “Oak “. In 1995, this language was renamed as “JAVA” due to legal
problem. Inventor of the language are James Gosling, Pattrick Naughton, Arthur Van and others. Java
was invented for the development of software for cunsumer electronic devices like TVs, tosters, etc.

1.2 JAVA FEATURES:


Features of Java are as follows:
1. Compiled and Interpreted
2. Platform Independent and portable
3. Object- oriented
4. Robust and secure
5. Distributed
6. Familiar, simple and small
7. Multithreaded and Interactive
8. High performance
9. Dynamic and Extensible

1. Compiled and Interpreted


Java language is both compiled and interpreted. Java compiler translates Java code to Bytecode
instructions and Java Interpreter generate machine code that can be directly executed by machine.
2. Platform Independent and portable
Java is portable language. Java programs can be easily moved from one computer system to another and
anywhere. Changes and upgrades in operating systems, processors and system resources will not force
any alteration in Java programs. This platform Independent nature of java language makes it as the best
choice for internet programming as on Internet which interconnects different kind of systems worldwide.
Java certifies portability in two ways.
• Java compiler generates the bytecode and that can be executed on any machine.
• Size of primitive data types are machine independent.

3. Object- oriented
Java is truly object-oriented language. In Java, almost everything is an Object. All program code and
data exist in objects and classes. Java comes with an extensive set of classes; organize in packages that
can be used in program by Inheritance.

4. Robust and secure


Java provides many securities to make certain reliable code. It has strict compile time and run time
checking for data types. It is design as garbage collected language, which relieving the programmers
from all memory management problems. Java also includes the concept of exception handling, which
deals with serious errors and reduces threat of crashing the system. Security is an important feature of
Java. The absence of pointers in Java ensures that programs cannot access to memory location without
proper approval.

Shivaji Science College, Nagpur


BSC –II Semester-IV

5. Distributed
Java is called as Distributed language for applications on networks which can contribute both data and
programs. Java applications can open and access remote objects on Internet easily. That means multiple
programmers at multiple remote locations to work together on single task.

6. Simple and small


Java is very small and simple language. Java does not use pointer and header files, goto statements, etc.
It eliminates operator overloading and multiple inheritance.

7. Multithreaded and Interactive


Multithreaded means managing multiple tasks simultaneously. Java maintains multithreaded programs.
That means we need not wait for the application to complete one task before starting next task. This
feature is helpful for graphic applications.

8. High performance
Java performance is very extraordinary for an interpreted language, majorly due to the use of
intermediate byte code. Java architecture is also designed to reduce overheads during runtime. The
incorporation of multithreading improves the execution speed of program.

9. Dynamic and Extensible


Java is also dynamic language. Java is capable of dynamically linking in new class, libraries, methods
and objects. Java can also establish the type of class through the query building it possible to either
dynamically link or abort the program, depending on the reply.
Java program is support functions written in other language such as C and C++, known as native
methods.

1.3 COMPARISON IN JAVA AND C++

Java C++
1. Java does not support operator C++ supports operator overloading.
overloading.
2 Java does not support multiple C++ supports multiple inheritance of
inheritance of classes.
classes but it supports interface.
3 Java does not support global variable. C++ support global variable.
Every
variable should declare in class.
4 Java does not use pointer. C++ uses pointer
5 There are no header files in Java. We have to use header file in C++.

1.4 JAVA VIRTUAL MACHINE:

The Java Virtual Machine (JVM) is a platform-independent engine that does not exist, used to run Java
applets and applications. The JVM understands the particular file format of the platform and
implementation independent class file produced by a Java compiler. Therefore, class files produced by a

Shivaji Science College, Nagpur


BSC –II Semester-IV

Java compiler on one system can execute without change on any system that can invoke a Java Virtual
Machine. Java compiler convert the source code into intermediate code is called as bytecode. The
bytecode is not machine specific. The machine specific code is generated by Java interpreter by acting
as an intermediary between the virtual machine and real machines

1.5 JAVA ENVIRONMENT:

Java environment includes a number of development tools, classes and methods. The development tools
are part of the system known as Java Development Kit (JDK). The classes and methods are part of the
Java Standard Library (JSL), also known as the Application Programming Interface (API).

1.5.1 Java Development kit (JDK) includes:

1. Appletviewer : It is used for viewing the applet)


2. Javac : It is a Java Compiler which converts java code into bytecode
3. Java : It is a java interpreter which runs the application by reading and interpreting
bytecode.
4. Javap : Java diassembler,which convert byte code into program description.
5. Javah : It is for java C header files
6. Javadoc : It is for creating HTML document.
7. Jdb : It is Java debugger which finds error in program

1.5.2 Application Programming Interface (API) includes:


1. Language Support Package : It includes collection of classes and methods required for
implementing basic features of java.
2. Utilities Package : It includes collection of classes for utility functions such as date and time.
3. Input/Output Package : It includes collection of classes required for input/output
implementation.
4. Networking Package : It includes collection of classes for communicating with other
computers via internet.
5. AWT (Abstract Window Toolkit) Package : It includes collection of classes to implements
graphical user interface.
6. Applet Package : It includes collection of classes to create java applets.

1.6 JAVA Programming:


There are two types of java programs:
1. Stand alone applications : These are programs in java to carry out specific task on stand alone
local computer. Execution of it involves two steps.
i) Compiling source code into bytecode using javac compiler.
ii) Executing the bytecode using java interpreter.
2. Applets : These are small programs in java developed for Internet Applications. An applet located
on server can be downloaded via internet and executed on local computer using internet browser.
We can develop applets for animated graphics, games and utilities.

1.7 JAVA PROGRAM STRUCTURE :


Shivaji Science College, Nagpur
BSC –II Semester-IV

A java program structure may contain one or more following sections.

Document Section optional


Package Statement optional
Import Statements optional
Interface Statements optional
Class Definitions optional
Main Method Class Compulsory
{
Main method definition
}

Document Section : it contains comment line (// or /*..*/).


Package Statement : The first statement allowed in a java file is package statement. It declares a
package name and inform the compiler that the classes defined here belong to this package. (
package student;)
Import Statements: After package, there may be number of import statements. (import [Link];).
It instructs the interpreter to load the test class contained in the package student.
Interface Statements: It is like class includes only abstract methods. It is used only in multiple
inheritance.(interface Sports{ …} )
Class Definitions: A java program may contain multiple class definitions. These are essential
elements of java program. (class Sports{ …} )
Main Method Class: Every java program requires a main method as starting point. The main method
creates objects of various classes and establishes communication between them. At the end of main
method, program terminates and control returns back to operating system.

1.8 Simple Java Program:


//This is my first program
class SampleProgram
{
public static void main(String args[])
{
[Link](“ Welcome “);
}
}
The file must be named [Link] to equivalent the class name containing the main
method.
Java is case sensitive. This program defines a class called “SampleProgram”.
A class is designed to perform a specific task. A Java class is defined by its class name, an open
curly brace, a list of methods and fields, and a close curly brace.
The name of the class is made of alphabetical characters and digits without spaces, the first
character must be alphabetical.
Shivaji Science College, Nagpur
BSC –II Semester-IV

The line “public static void main (String[] args )” shows where the program will start running.
The word main means that this is the main method – The JVM starts running any program by
executing this method first.
The main method in “[Link]” consists of a single statement
[Link]("Welcome");
The statement outputs the character between quotes to the console.
For compiling and running the program we have to use following
commands:
a) javac (Java compiler)
Java compiler convert the java program into bytecode. It converts “.java” file in “.class” file.
Syntax:
C:\javac [Link];
It creates [Link] file.
b) java(Java Interpreter)
Java interpreter interprets bytecode file on JVM , generate machine specific code and execute it.
Syntax:
C:\java abc
It interprets [Link] file and execute it.

1.9 Tokens in Java


A Java program is basically a set of classes. A class is defined by a set of declaration statements and
methods or functions. Most statements contain expressions, which express the actions carried out on
information or data. Smallest individual thing in a program are known as tokens. The compiler
recognizes them for building up expression and statements.
There are five types of token as follows:
1. Literals
2. Identifiers
3. Keywords
4. Operators
5. Separators
1.9.1 Literals:
Literals are constant values to be stored in variables. Java language specifies five major types of literals
are as follows:
1. Integer literals
2. Floating point literals
3. Character literals
4. String literals
5. Boolean literals

1.9.2 Identifiers:

Identifiers are programmer-created tokens. They are used for naming classes, methods, variables,
objects, labels, packages and interfaces in a program. Java identifiers follow the following rules:
1. They can have alphabets, digits, and the underscore and dollar sign characters.
2. They must not start with a digit.
3. Uppercase and lowercase letters are individual.
4. They can be of any length.
Shivaji Science College, Nagpur
BSC –II Semester-IV

Identifier must be meaningful, easily understandable and descriptive.


Java developers have followed some naming conventions( Not Rules).

Names of all public methods and instance variables start with leading lowercase letter. Examples :
calculate, total, name etc.
When more than one words are used in names , second and subsequent words are marked a leading
uppercase letters. Example : addTotal, myHeight etc.
All private and local variables use only lowercase letters combined with underscores. Examples :
length, batch_strength etc
All classes and interfaces start with leading uppercase letter. Example : Student, MotorCycle etc.
Variables that represent constant values use all uppercase letters and underscores between words.
Example : SIZE, MAX_VALUE

1.9.3 Keywords:
Java language has reserved 50 keywords. Keywords have specific meaning in Java. We cannot use
them as variable, classes and method. Examples : abstract, char, catch, boolean, default, finally, do,
implements, if, long, throw etc.

1.9.4 Operator:
An operator is symbols that specify operation to be performed may be mathematical and logical
operation. Operators are used in programs to operate data and variables. These are part of mathematical
or logical expressions.
Categories of operators are as follows:
1. Arithmetic operators
2. Logical operators
3. Relational operators
4. Assignment operators
5. Conditional operators
6. Increment and decrement operators
7. Bit wise operators

1.9.5 Separator:
Separators are symbols. It shows the separated code.

Name Use
() Parameter in method definition, containing statements for conditions etc.
{} It is used for define a code for method and classes
[] It is used for declaration of array
; It is used to show the separate statement
, It is used to show the separation in identifier in variable declarations
. It is used to show the separate package name from subpackages and classes,
separate variable and method from reference variable.

1.10 DATA TYPES:


The data type defines the types of data that is represented by a variable.
There are two types of data types.
Shivaji Science College, Nagpur
BSC –II Semester-IV

1. Primitive data type : there are two categories


i. numeric means Integer, Floating points
ii. Non-numeric means Character and Boolean
2. Non-pimitive data types: There are three categories
i. Classes
ii. Arrays
iii. Interface

Following table shows the data types with their size and ranges.

Data type Size (byte) Range


byte 1 -128 to 127
boolean 1 True or false
char 2 A-Z,a-z,0-9,etc.
short 2 -32768 to 32767
Int 4 -2 million to 2 million
long 8 -10E18 to 10E18
float 4 -3.4E38 to 3.4E18
double 8 -1.7E308 to 1.7E308
1.10.1 Integer data type:
Integer datatype can hold positive number or negative number. There are four
types of integer as follows:
a) byte :
b) short
c) int
d) long
We can make integer long by adding ‘l’ or ‘L’ at the end of the number.

1.10.2 Real data type (Floating point) :


There are two types of floating point data type.
a) Float : It represents single precision numbers. it uses 4 bytes for storage space. It is very useful for
small degree of precision.
b) Double : It represents single precision numbers. it uses 8 bytes for storage space. It is very useful
for large degree of precision.
1.10.3 Character data type:
It is used to store single character in memory. It uses 2 bytes storage space.

1.10.4 Boolean data type:


There are only two values that a boolean type can hold: true and false. Boolean type is denoted by the
keyword boolean and uses only one bit of storage.

1.11 MIXING DATA TYPES:


Java allows mixing of constants and variables of different types in an expression. When computer
consider operand and operator and if operands are different types then type is automatically convert in
higher type.
Following table shows the automatic type conversion.
char byte short int Long float double
Shivaji Science College, Nagpur
BSC –II Semester-IV

char Int int int int Long float double


byte Int int int int Long float double
short Int int int int Long float double
int Int int int int Long float double
long Long long long long Long float double
float Float float float float Float float double
double double double double double double double double

1.12 VARIABLES:
A variable is an identifier that denotes storage location. A variable can be used to store a value of any
data type. A variable must be declare in the program before using it. A variable may take different values
at different times during the execution of program.
Rules of Variable names :
A variable name may consists of
• Use alphabets, digits, underscore( _ ), and dollar character( $)
• A name cannot include the space character.
• Do not begin with a digit.
• A name can be of any realistic length.
• Upper and lower case count as different characters.
• A name cannot be a reserved word (keyword).

Example : length, total_quantity, materialStrength

1.12.1 Declaration of variable :


Syntax:
Data-type variablename1, variablename2,….variablenamen;

Example : int basic;


Float tax, percent;

1.12.2 Initialization of variables :


There are two ways:
i) By using assignment statement
Syntax :
variableName=value;
Example :
count=10;
i=1;
or

At the time of declaration


Syntax : dataType variableName=value;

Example :
int count=10;
float tax=1.7f;

Shivaji Science College, Nagpur


BSC –II Semester-IV

ii) By using a read statement (Keyboard Input)


In java, any input is read in as string. The readLine method is used to input the data in variable.

1.12.3 Scope of variables : There are three types of variables


1. Instance variables
2. Class variables
3. Local variables
1. Instance variables : It is declared in the class. These are created when the objects are instantiated and
are associated with objects. They take different values for each object.

2. Class variables : Class variables are declared in inside class. These are global to a class and belong to
entire set of objects. Only one memory location is created for each class variable.

3. Local variables : Variables declared inside methods are called local variables. These are not available
outside the method. Local variables can also be declared inside program blocks (opening and closing brace).
These variables are visible only in the program block. When the program control leaves the block, all the
variables in the block will erase.

1.13 Constant:
Constant means fixed value which is not change at the time of execution of program. Java supports
different types of constant as follows:

1.13.1 Integer Constant:


An Integer constant refers to a series of digits. There are three types of integer as follows:
a) Decimal integer : It allows 0-9 digits and optional – sign.
Example : Valid integer constants are 234, -555, 0
Embedded spaces, commas and characters are not allowed in between digits.
For example: Invalid integer constants are
34 722
4,00,000
34.99
b) Octal integer
It allows us any sequence of numbers or digits from 0 to 7 with leading 0 and it is called as Octal
integer.
For example: Valid octal constants are
022
00
0677
c) Hexadecimal integer
It allows the sequence which is preceded by 0X or 0x and it also allows alphabets from A to F or a to f
( A to F stands for the numbers 10 to 15 ) it is called as Hexadecimal integer.
For example: Valid hexadecimal constants are
0x7
0X3F

1.13.2 Real Constant:


Shivaji Science College, Nagpur
BSC –II Semester-IV

It allows fractional data and it is also called as floating point constant.


For example: Valid Real constants are
0.0234
0.777
-1.23
1.13.3 Single Character Constant
It allows us single character within pair of single quote.
For example: Valid single character constants are
‘A’
‘8’
‘/’
1.13.4 String Constant
It allows us the series of characters within pair of double quote.
For example: Valid string constants are
“Hello”
“Good Bye”

1.13.5 Symbolic constant:


In Java program, there are many things which is requires repeatedly and if we want to make changes then
we have to make these changes in whole program where this variable is used. For this purpose, Java
provides final keyword to declare the value of variable as follows:

Syntax:
final type Symbolic_name=value;
For example:
final float PI=3.1459;

1.13.6 Backslash character constant:


Java support some special character constant which are given in following table.

Constant Meaning
‘\n’ New line
‘\f’ Form feed
‘\b’ Back space
‘\t’ Horizontal tab
‘\r’ Carriage return
‘\’’ Single quote
‘\”’ Double quote
‘\\’ backslash

1.14 Comments:
A comment is a remark in a program. It is not executable statement.
Single line comment : // This is my first program
Multiple line comment : /*
Fdhdfhh
Cvnxcvncn
Shivaji Science College, Nagpur
BSC –II Semester-IV

*/
1.15 Command line arguments:
Command line arguments are parameters that are supplied to the application program at the time of
execution following the file name.
In the main () method, the args is confirmed as an array of string known as string objects. Any argument
provided in the command line at the time of program execution, are accepted to the array args as its
elements. Using index or subscripted entry can access the individual elements of an array.
The number of element in the array args can be getting with the length parameter.
For example:
class Add
{
public static void main(String args[])
{
int a=[Link](args[0]);
int b=[Link](args[1]);
int c=a+b;
[Link](―Addition is=‖+c);
}
}
output:
c:\javac [Link]
c:\java Add 5 2

1.16 Operator:
An operator is symbols that specify operation to be performed may be certain mathematical and logical
operation. Operators are used in programs to operate data and variables. These are a part of mathematical or
logical expressions.
Categories of operators are as follows:
1. Arithmetic operators
2. Logical operators
3. Relational operators
4. Assignment operators
5. Conditional operators
6. Increment and decrement operators
7. Bit wise operators
8 Instanceof Operator
9 Dot Operator

1.16.1 Arithmetic operators: It is used to construct mathematical expression.


Operator Meaning
+ Addition or unary plus
- Subtraction or unary minus
* Multiplication
/ Division
Shivaji Science College, Nagpur
BSC –II Semester-IV

% Modulo Division(Remainder)

Example : Suppose a= 18 and b=4


a+b =22 , a-b =14, a*b =72, a/b =4, a%b =2

1.16.2 Logical operators:


When we want to form compound conditions by combining two or more relations, then we can use logical
operators. Following table shows the details of logical operators.

Operator Meaning
|| Logical - OR
&& Logical - AND
! Logical - NOT
Example : a>b && z==9

1.16.3 Relational Operators:


When evaluation of two numbers is performed depending upon their relation, assured decisions are
made. The value of relational expression is either true or false.
Following table shows the details of relational operators.

Operator Meaning
== Equal to
> Greater than
< Less than
!= Not equal to
>= Greater than or equal to
<= Less than or equal to

1.16.4 Assignment Operators:


Assignment Operators is used to assign the value of an expression to a variable.
Variable_name binary_operator = expression
Example : x +=y+1 i.e. x=x+(y+1) , z+=1 i.e. z=z+1

1.16.5 Conditional Operators:


The character pair ?: is a ternary operator of Java, which is used to construct conditional expressions of
the following form:
Expression1 ? Expression2 : Expression3
The operator ? : works as follows:
Expression1 is evaluated if it is true then Expression2 is evaluated and becomes the value of the
conditional expression. If Expression1 is false then Expression3 is evaluated and its value becomes the
conditional expression.

Example : Suppose A=3; and B=4;


Shivaji Science College, Nagpur
BSC –II Semester-IV

C=(A<B)?A:B;
C=(3<4)?3:4;
C=4

1.16.6 Increment and Decrement Operators:


The increment operator ++ adds 1 to a variable. Usually the variable is an integer type, but it can be a
floating point type. The two plus signs must not be split by any character. Usually they are written
immediately next to the variable.
Following table shows the use of operators.
Expression Process Example end result
A++ Add 1 to a variable after use. int A=10, B; A=11 B=10
B=A++;
++A Add 1 to a variable before use. int A=10, B; A=11 B=11
B=++A;
A-- Subtract 1 from a variable after use. int A=10, B; A=9 B=10
B=A--;
--A Subtract 1 from a variable before use. int A=10, B; A=9 B=9
B= --A;

1.16.7 Bit Wise Operators:


Bit wise operator execute single bit of their operands. Following table shows bit wise operator:
Operator Importance/ significance
| Bitwise OR
&= Bitwise AND assignment
& Bitwise AND
|= Bitwise OR assignment
^ Bitwise Exclusive OR
<< Left shift
>> Right shift
~ One‘s complement

1.16.8 Instanceof Operator : It is an object reference operator and returns true if the object on the left
hand side is instance of the class given on the right hand side. This operator allows us to find whether the
object belongs to a particular class or not.
Example : account instanceof BankAccount is true if the object account belongs to the class BankAccount
otherwise it is false.

1.16.9 Dot Operator : It is used to access the instance variables and methods of class objects.
Example : [Link]
[Link]()

1.17 Separator:
Separators are symbols. It shows the separated [Link] describe function of our code.

Shivaji Science College, Nagpur


BSC –II Semester-IV

Name use
() Parameter in method definition, containing statements for conditions,etc.
{} It is used for define a code for method and classes
[] It is used for declaration of array
; It is used to show the separate statement
, It is used to show the separation in identifier in variable declarartion
. It is used to show the separate package name from sub-packages and classes, separate
variable and method from reference variable.

1.18 Operator Precedence in Java:


An arithmetic expression without any parentheses will be calculated from left to right using the rules of
precedence of operators.
There are two priority levels of arithmetic operators are as follows:
(a) High priority
(b) Low priority
The evaluation process includes two left to right passes through the expression. During the first pass, the
high priority operators are applied as they are encountered.
During the second pass, the low priority operators are applied as they are encountered.
For example:
Z=A-B/3+C*3-1
When A=10, B=13, C=3

First pass:
Z=10-(13/3) + (3*3)-1
Z=10-4+3-1

Second pass:
Z=6+3-1
Z=7
Answer is=7
Following table shows associativity of operators.

Operator Operation Associativity Precedence level

[] Array declaration Left to right 1


() Function call

. Object member selection


- Unary minus Right to left 2
++ Increment
-- Decrement
! Logical negation
~ 1’s complement
(data type) Casting
% Modulus Left to right 3
Shivaji Science College, Nagpur
BSC –II Semester-IV

* Multiplication
/ Division
+ Addition Left to right 4
- Subtraction
>> Right shift Left to right 5
<< Left shift
>>> Right shift with zero fill
< Less than Left to right 6
<= Less than or equal to
> Greater than
>= Greater than or equal to
Instance of Type comparison
== Equality Left to right 7
!= Inequality
& Bitwise AND Left to right 8
^ Bitwise XOR Left to right 9
| Bitwise OR Left to right 10
&& Logical AND Left to right 11
|| Logical OR Left to right 12
?: Conditional Operator Left to right 13
= Assignment operator Left to right 14

1.19 Input/ Output Statements :


i) Console output : The out object in the system class represents the standard output stream. The out
object has print() and println() methods. The print() method displays the output on the screen and
retains the cursor on the same line. The println() method display the output on the screen and moves
the cursor to the beginning of the next line.
Example : [Link](“Welcome”);
[Link](“Good Morning “+name);
We can print blank line by using a println() method.
Example : [Link]();

ii) Keyboard Input :


In java, any input is read in as string. The readLine method is used to input the data in variable.

[Link](“Hello, What is your Name ? “);


InputStreamReader reader= new InputStreamReader([Link]);
BufferedReader in =new BufferedReader(reader);
String text =[Link]();

The name that is keyed in by the user is received by the system and stored in the buffer [Link] in
terms of bytes.
In the second statement , the bytes in the buffer [Link] are converted into characters and stored in the
object reader. An inputStreamReader object reader can read characters but can not read the whole string
at a time.
To overcome this, in third statement, inputStreamReader data transferred into buffered object in.
In the fourth statement , the readLine() method reads the data and store in string variable text.
Shivaji Science College, Nagpur
BSC –II Semester-IV

For Number input:

[Link](“Hello, What is your Name ? “);


InputStreamReader reader= new InputStreamReader([Link]);
BufferedReader in =new BufferedReader(reader);
String text =[Link]();
int basicSalary=[Link](text);

The parseInt method of the class Integer, take out the integer from string text and store it in variable
basicSalary.

Simple Program
Import [Link].*;
class HelloDemo
{
public static void main( String args[]) throws IOException
{
[Link](“Hello, What is your Name ? “);
InputStreamReader reader= new InputStreamReader([Link]);
BufferedReader in =new BufferedReader(reader);
String name =[Link]();
[Link]( "My name is "+ name);
}
}

1.20 Type Casting : The process of converting one data type to another is called casting.
Example :
Int j=20;
Byte k=(byte) j;
Long x= (long)j;
Integer type can be cast to any other type except Boolean. Casting in to smaller type may result in loss
of data. Float and Double can be cast to any other type except Boolean. Casting a floating point to an
integer in a loss of fractional part.

The following table shows casting that results in no loss of information

From To
char Int, long , float,Double
byte Short, char, int, long, float, double
short Int, Long, float, double
int Long, float, double
long Float, double
float double

Shivaji Science College, Nagpur

You might also like