Adobe Scan 05-Dec-2024
Adobe Scan 05-Dec-2024
Java is said to be distributed. Since it enables the user to creates applications on network.
5. Robust and Secure
Java is said to be Robust and secure since it provides exception handling mechanism and safeguards, performs
strict compile time and run time checks and it prevents unauthorized aCCess.
Q17. Discuss in brief about
its types. Data types. Explain
Important Like Water
Answer:
Data Types
Data types are mainly used
in Java since, it is a
strongly
language. Java, compiler evaluates
In typed
all the
variables, values and expressions in order to type of
Iype conmpatibility. maintain
Meanwhile,
reliability is increased. errors are reduced and
Java supports two different data types.
as follows, They are
1.
Primitive Datatype
Primitive datatype is one of thesimplest datatype
that consist of a single value.
Java supports eight types
of primitive datatypes which are
as showWn below, grouped into four types
(i) Integer Data Type
The integer type can represent the signed integer
values that may be positive or negative. Java does
not allow unsigned integer values since, they are not
required. This data type itself consists of four different
lypes. 1hey are as follows,
10
(a) PROGRAMMING IN JAV
bvte System.out.println(°The value of byte
The
hie data t\pe is datatype is:"
\alues that have a size otused1hvte
to maintan integeT
System.out.println(Thevalue of sho.
X)
Tange ot byte data Ivpe is fhom Le., Sbts. The
128 to 127. datatype is:"
Syntax: bvte variable name: System.out.printIn(*The value of int
y);
(b)
Example: hyte datatype is:" 2);:
short System.out.println("The value of long
The short data tvpe datatype is:" +p;
\alues and the size Npesents the signed integer
of this data type is
e, l6 bits. The range ot 2 bytes
this data tvpe is trom
Sy ntax: short Output
variable name:
Example: short a. b: GA Command Prompt
C:Users \CG3>javac IntegerExamplejava
(c) nt
C:\Users\CG3>java
The value of byte IntegerExample
The int datatype is most The value of short datat ype is 2
datatype
nteger datatvpes. Since,pretferable
is :23456
among allthe
it can control index of
The.value of int
The value of long datatype isis:117438
arTa\S and loops. datatype :732227363054
The size of this data tvpe is C:Users\CG3>
bits and the range is from 4 bytes i.e., 32 (ii)
Floating-point Data Type
147483.64 -2,147,483.648 to Floating-point typecan be defined as a data type
Syntax: int variable name: which indicates the fractional values. The two types of
Example: int a. b: floating-pointdata types are as follows,
(a) float
a=23767S9;
d)
long The float data type is
The long dataty pe is a signed 8 precision numbers and usedthe
to represent
size of this datasingle
byte (64 bit) which is 4 bytes i.e., 32 type
has a range trom - bits. The
lo
9,223,372.036,854,775.808 float literal is maximum value of
9,372. 036.854,775,807.
data type is used to Generally, this approximately
Syntax: float variable
3.4 x 10*,
that hae arange manage the integer values name;
greater than int.
Syntax: long variable name:
Example: float x=3.14F;
Program on float
-Example: long x.y : Import java.io,*:
\=123456789 10L: class FloatExample
ogram on Integer Data Tvpe
Lmport java.io.*:
class IntegerExample public staticvoid
main(String |args)
publi static void main( String ] float x;
args)
byte x
X=3.14F;
short
nt Z.
System.oul.println(The float value oe
is:"+ ):
long p
Output
y-23456
Command Prompt
!-117438, ENUsers \CGS>Jav ac
Float
732227363054L;
SIA C:Users\CG3>jvalueava Float ExaExanpl
The flo at
of mple e.java
x is
:3.14
UNIT-1:Inhoduction 11
double Program on Character Data Type
(b)
Double data type is uscd to represcnt double import java io,:
nnwision numbers and the size of this data typc class CharacterExample
isSbytes i.c., 64bits, The largest value of double
literal is approximately I8 3 The double
data typc is frequently uscd in java since, the public static void main(Stringl | args)
math functions in Javaclass library uses double
values. char y 'N':
Syntax: double variable name: System.out.println(The valuc of y
is:" y):
Example: double x: y;//The char value is decremented
Program on double System.out.println("Thenew value
import java.io.*: ofy is:" y):
y= 82; I/The char data type can be
class DoubleExample llassigned with integer
System.out.println(The another
value of y is:" + y):
public static void main(String |]args)
Kitien
Canditin
clsc
ateIent
atement
Satemet-1t
c=9:
Syntax ifa>b)
if conditon-2
ifa>c)
ificondi
System.out. printin("a is greater"):
else
statemen:-2.
else
itib >)
clse
System.out. println("b is greater");
statement-5.
else
statement-next.
Sy stem.out.println("c is greater:
If cond1tion- 1is truc. then condition-is exeCuted
otherw ise. statement-3 will be evevuted followed by
statement-next. If condition-2 is cv aluated to truc then
statement-1 is executed or statement- is C\ecuted
tollow ed by statement-nexi in both cascs
Warning Xerox/Photocopying of this book is aCRIMINAL act. Anyone found guity is LIABLE to face LEGAL proceedings
Output
CWhindosisitend emd evt NestedExanple.Java
CEsers\sure\lesktoy NLabuerk) javac
CEVBsers\sUree\lesktoy\labuork)java NestedExanple
is greater
\Users\sUre xDesktop\Labuork
(d) if-else-if Ladder sequence
The if-else-if ladder can be defincd as a
of if-else statements.
Syntax
iflcondition-)
statement-1:
clse ificondition-2)
statement-2:
else ifj condition-3)
statement-3:
else if<condition-k)
statement-k:
else
statement-k+1:
statement-next:
The conditions are evaluated sequentially (i.e..
irom top to bottom). If a particular condition is false
then the next following conditions are evaluated. If a
condition is satistied, then its corresponding statement
IS executed.
True False
Statemur-! Condition-2
Irue
Statenwnt-2 Conditivn-3 False
Default
`tatenent-3
statennt
Statenail-neM
(b) Constructor.
Instance Variable Initializer
(a)
instance variable which is declared outside
The instance variable initializer assigns values directly to any
the function/method but inside the class.
b) Constructor
object is created, constructor is automatically
Constructor is often used to initialize the objects. When an be
default constructor with no arguments and body will
invoked. If constructor is not created manually, a below statement.
constructor is invoked after the execution of
created by java compiler. This default
new Student( );
always same.
The name of both constructor and class is
Accessing Class Members
of a class using dot(.) operator.
An object can access the members
Syntax
Objectname.membername;
Example
objStudent.stdld = 101;
objStudent.getStdld(0; functions and stdld is
."is the operator used to access member
name of the object,
Here, obiStudent is the which is to be invoked.
variable. Finally getStdld( ) is the function
an instance
Program
public class Prog
text);
System.out.printIn("Given word is :"t
proceedings.
found guilty is LIABLE to face LEGAL
CRIMINAL act.Anvone
Xerox/Photocopying of this book is a
Warning :