0% found this document useful (0 votes)
16 views11 pages

Adobe Scan 05-Dec-2024

The document outlines key features of Java, highlighting its object-oriented nature, platform independence, and robust security mechanisms. It also discusses Java's data types, including primitive and non-primitive types, and explains type casting and control statements in Java programming. Overall, it serves as a comprehensive overview of Java's capabilities and functionalities.
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)
16 views11 pages

Adobe Scan 05-Dec-2024

The document outlines key features of Java, highlighting its object-oriented nature, platform independence, and robust security mechanisms. It also discusses Java's data types, including primitive and non-primitive types, and explains type casting and control statements in Java programming. Overall, it serves as a comprehensive overview of Java's capabilities and functionalities.
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
You are on page 1/ 11

Write about different features of Java.

Answer Nov./Det.. 19, 01 (ou


Features of Java
and popular prOgrarnmino
Java possess the following features which makes java powerful, objccl-oricnted
language.
1.
Object-oriented
object model 1s simple and can
Java is said to be object oricnted, since everything in it is object based. Its
be casily extended.
2. Compiled and Interpreted
ofcompiler and interpreter.
Java is said to be compiledand interpreted since it combincs thc fcatures
3. Platform Independent and Portable
any computer
Javais said to be platform independent and portabje since its program can be moved and run on
platform.
4. Distributed

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)

double x.y: Output


X= 25: C Command Prompt

y= Math.sqrt(x): C:\Users \CG3 )javac CharacterExanple.java


C:Users\CG3)java CharacterExample
System.out.printin(*The double value The value of y is:N
The new value of y is :M
of y is:" + y); The another value of y is :B
C:\Users\CG3>

(iv) Boolean Data Type


the
The boolean data type indicates whether
Output value of an expression is either true or false. Therefore,
declared as boolean
a variable or expression which is
G Command Prompt type can use these two keywords.
C:\Users \CG3>javac DoubleExample.java
C:\Users\CG3 >java Double Exanple Syntax
The double value of yis:5.0
boolean variable name;
C:\Users\CG3> Example
boolean a;
(iii) Character DataType a =true;

The character (i.e., char) data type represents (or)


16-bits (2
characters and the size of this data type is a = false;
it supports
bytes). Java language uses 2 bytes since Program on Boolean Data Type
characters can be defined
unicode characters. Unicode
all the characters import java.io.*;
as aset of characters that indicates
character data type stores class BooleanExample
of human languages. The 65,536.
of 0to
unsigned l6-bitcharacters with a range single quotes.
in
The character value must be enclosed public static void nmain( Stringl |args)
Syntax
char variable name; boolean z,a;
int x, y:
Example
X10;
char x;
20;
x ='n':;
LIABLE to face LEGAL proceedings.
Xerox/Photocopying of this book is a
CRIMINAL act.Anyone found guilty is
Warning :
12 PROGRAMMING IN JAVA
performed on
cannot be
iflz >) T'ype casting
variables. Boolcan
T'ype casting ofinteger datatype into any other
datatype is possible. But, if the casting int
a false: smallertype is perlormed, it results in loss of
S\sem.out.println'a is:" a): data.
Sstem.out.println(x is not point types into
3 Type casting of floating possible, butother
is
grcater than y"): float types or integer type
loss of data.
types :
if\z =y>x) 4. Type casting of char type into integer sin
possible. But, this also results in loss of data,
char holds 16-bits the casting of it into byte thot
a-true: results in loss of data or mixup characters.
System.out.printin(a is :"ta): Syntax
(target-type) expression;
System.out.println("y is greater Here, target-type is the specification to convem
than x"):
the expressioninto required type.
Example
float a, b;
int c= (int) (a + b):
Output The result of float type variables can be converted
in to integer type explicitly. The parentheses for the
G Command Prompt
expression a+ bis necessary. Otherwise, the variable
C:NJsers\CG3 > javac Boo lean Exanple.java can be converted into int but not the result ofa+b. The
C:NUsers\CG3>java
a is ' :true Boo le an Exanp le casting of double to int is necessary since, they are not
y is compatible.
greater than x
Furthermore, Java performs assignment of
C:Users\CG3) value of one type to a variable of another type
2
Non-primitive Datatype without performing type casting. Here the conversion
is performed automatically which is referred to as
Non-primitive datatypes are also called as automatic type conversion. It can take place when
reference data types. These datatypes include classes destination type hold sufficient precedence to store
object and interfaces references. SOurce value.
Class Object Declaration Example
The syntax for declaration of class object i given byte b= 50;
as follows, int
valid statement
a =b:
Class identifier Object identifier The assignment of smaller
Interface Reference Declaration type is reterred to as widening ordatatype to larger data
promotion and the
assignment larger type value to smaller
The syntax for declaration of an interface is referred to as
reference is given as follows,
narrowing (it results in data loss).
While
performing the conversion between two
Interfacc identifier reference identifier incompatible type, the use of word
is an explicit type cast is necessary. It
1.3 TYPE CASTING Syntax conversion.
18. Explain about the type casting and type (larget type) value target type.
conversion. It is a desired type lo
Answer: While convert the given value.
the narrowing casting the types, it is necessary to
ype Casting
long value is conversion which causes data lost.prevenl
If the
Typecasting' is an explicn conversion of a value chance to loss converted
of data.
t0 short then there may be a
fone type into another type. And simply, the
data valuc 3.25 is Similarly, when
casted to an int value. then thea
floatine-pont
stated using parenthesis beforc the
valuc. Iype ype be converted to 3 and
castng value will
Java must follow the given rules. I is
beller to avoid remaining daa 0.25
d SIA
loss of data. narrowing conversions will be los.
to prevet
PUBLISHERS AND DISTRIBUTORS PVT. LTD.
14

1.4 cONDITIONAL STATEMENTS, Expression False


LOOPS
state- condhtion
Q19 Explain the various types of control
for each
ments in Java with an example
09(a) (OU] Truc
Answer Nov /Dec -19, |Statement-bhck
ControlStatements
statements in Java
The varous I\pCs of control Statement-next
anc as follows.
ofSimple if Statement
1. ConditionalStatements Figure:Flow-chart
20.
For answer refer Unit-I. Page No. 14. Q.No. if else
extension of simple
(b) is an
2 IterativeLoop Statements This statement

For answer refer Unit-1. Page No. 17. Q.No. 21.


statement.

3. Branching Jump Statements Syntax


if (condition)
For answer refer Unit-1. Page No. 20, O.No. 22.
Q20 Explain the Conditional Statements in statement-1;
Java with examples.
Answer May,June-19, Q4(a) (MGU)
else
Various types of conditional statements in java
are as follows.
statement-2:
1 if Statement
In Java, the if statement can be used in the
statement-next;
following forms. execu
Inthe above syntax, statement-1 willbe
(a) Simple if is tr
followed by statenment-next when the condition
(b) ifelse Otherwise, statement-2 will be executed followed
statement-next.
(c) Nested if else
(d) if-else-if ladder.
(a) Simple if
False
The if statement (or simple if statement) is Condition
used for decision making. It allows the computer to first
evaluate the condition and depending on its resultant
value it transfers the control to the corresponding True
statement in the program. This statement performs an Statement-1 Statement-2
action if the condition is true, otherwise it skips that
action and executes the other stalement.
Syntax Statement-next
if (condition) Figure: Flowchart of if-else Statement
Program
statement-1; import java.util.Scanner;
class EvenOrOdd
statcment next
Herc. if thecondition is satished, then statement public static void main( String args|)
block is exccutcd 1ollowedby statement-nex. Otherwise
statement-block IS skipped and Only statement-nexL is
CXecutcd. Int x:

ScanneT se new Scanner(System.in):


SIA
PUBLISHERS AND PVT.
UNT-1: introduction 15

Kitien

Canditin

clsc

ateIent
atement

Syssemprintin -s oád Tumber

Satemet-1t

Output Figure: Fawctart far Nesting af ifalse Statement


Program
AsersSreJesitepalunrd jaac nenrd-jasa Lmport javaio.*.
Nsers\reJesitopLaynri jana Fnenri mpot java util *:
Fter the alees ef x 19 pubic class NestedExample
NsersLsreJesktop\Lanr) public static void main( Sring |arzs)
Nested if else
inta b. c.
a=7;

c=9:

Syntax ifa>b)
if conditon-2
ifa>c)
ificondi
System.out. printin("a is greater"):

else

else System.out. printin "c is greater");

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.

Îue Condition-l False

True False
Statemur-! Condition-2

Irue
Statenwnt-2 Conditivn-3 False

Default
`tatenent-3
statennt

Statenail-neM

Figure: Flowchart of else-if Ladder


Programn
impon avaio:;
mport java. util.:
class Ladderl:xample

public static void main(String| |args)


1.5 CLASSES, OBJECTS, CLASS
DECLARATION, CREATING
OBJECTS
Q23. What is a class? How it is defined? How
are the member variables of a class
declared?
Answer:
Class
A class can be defined as a template that groups
data and its associated functions. A class contains the
instances such as variables and methods. It is responsible
for describing the behavior and data of these associated
instances. An object will be created when the class is
instantiated. The behavior of the objects or class will be
implemented using the methods. And, the data of them
will be stored in the variables.
Class Declaration

The class contains two parts namely,


(a) Declaration of data variables
(b) Declaration of member functions.
Theabovedeinition
22 slalName fet e
the adpetSdll md
Ihe data aplainsabout class, ntdld
),'setStlld ' are the member
membes of a clana uioneNplains tis
ate of the clast
And the nember are thee typs oflhecl54,
about the of the dash, Ihee methodis
)
of vaiablesbehaviout
available for aclass, They e as ollows,
In Java, maint ) defined
LocalVarlables declared | itsell.
Local vat iables are the vatialbles that are
Program
inide the methods
classSum
() Instance Variables
Instance variables Are the variables that are
declared inside the clans but outside of the publicstatic void main(String, argsl))
methods.
() Clas Variables
Class variables are the variables thbat are declared int x5, y 5;
inside thc class with static mnodifier and they
reside outside of the method. int res X y;
General Form of a Class System.out.printn(res);
Aclass is declared using 'class' keyword
followed by the name of the class. The syntax of a Class
is as follows,
Syntax
class narme of thc class Output
CWindewisytem2cmd.ee
lldeclaration of Instance variables Tier SiuresDektopslabvork3avac Sun
type data variablel;
type data variable2;
CENIkerxaIreS Deakton\lahuorkjava Sun
lldeclaration of functions llsereere Degktoplabvork)
type function namel(arg list)
Q24. Give a brief description about object. Als
body of function explain how an object can be declar
and initialized.
type function Answer :
name2(arg_ list)
Object
body of function An object can be
defined as an instance ofa cla
which is used to access the
members of a class.
It is better to
maintain information of one logical
Declaration
entity in a class. An object is similar to
Example that of varjable declaratit
Syntax
Consider the below code that defines a class.
class Student name of the class
Example object name;
int stdld;
int stdNane;
Student objStudent;
Here, Student is
void getStdid( );
int setStdld(int sid); objfor Sobjtudentect. isAthe reference thevariable
the name which
of the class
represen
an object reference can
just to holdvaria ble objStudent is declare'
the address of the object.
do SIA Student objStudent’ null
UNIT-1: Introduction 23
|nitialization of Object
When a reference variable is declared, it is neceNsary to assign physical copy of the object to that variable.
This can be done by using 'new' operator. This operator allocatesmemory dynamically tostore instancevariables
using a single argument called as constructor call. Afer the memory allocation, new operator returns the address
of memory location to the class. This address can be stored in the reference variable which is created.
Syntax
class name object_name new class namc( ):
Example
Student objStudnet = new Student( ):
Here, an object named as objStudent is created and memory is allocated. Meanwhile, instance variables are
assigned with various values. However, it is required to start with initial values. If initial values are not specificd,
then default values are assigned to these instance variables depending on its datatype. The initial values are ass1gned
using the following mechanisms.
(a) Instance variable initializer

(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

public Prog(String text)

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 :

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy