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

Java Unit 1 (2021)

The document outlines key features of Java, including its simplicity, object-oriented nature, portability, security, robustness, and support for multi-threading. It also discusses the Java Virtual Machine (JVM) and various types of operators in Java, such as arithmetic, relational, and logical operators. Additionally, it covers Java's naming conventions and the three main parts of Java: Java SE, Java EE, and Java ME.

Uploaded by

venkata rajesh
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 views52 pages

Java Unit 1 (2021)

The document outlines key features of Java, including its simplicity, object-oriented nature, portability, security, robustness, and support for multi-threading. It also discusses the Java Virtual Machine (JVM) and various types of operators in Java, such as arithmetic, relational, and logical operators. Additionally, it covers Java's naming conventions and the three main parts of Java: Java SE, Java EE, and Java ME.

Uploaded by

venkata rajesh
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/ 52

Q) java features (OR) java Buzzwords?

Ans:

1.Simple:

The Java language is easy to learn and its coding style is easy to read and
write. It contains many features of other Languages like C and C++ and Java
removes complexity because it doesn't use pointers and doesn't support
Multiple Inheritance.

2.Object oriented:

In java, everything is an object which has some data and behaviour. Java can
be easily extended as it is based on Object Model. Following are some basic
concept of OOP's.

i. Object
ii. Class
iii. Inheritance
iv. Polymorphism
v. Abstraction
vi. Encapsulation

3.Portable:

Means able to be easily carried or moved. Write once, run anywhere (WORA)
feature makes it portable.

4.Secure:

With Java's secure feature it enables to develop virus-free, tamper-free


systems. Authentication techniques are based on public-key encryption.

5.Robust:

Robust simply means strong. Its capability to handle Run-time Error,


automatic garbage collection, the lack of pointer concept, Exception Handling
etc. makes java robust.

6.Architecture-neutral:

Java is architecture neutral because there are no implementation dependent


features, for example, the size of primitive types is fixed.
In C programming, int data type occupies 2 bytes of memory for 32-bit
architecture and 4 bytes of memory for 64-bit architecture. However, it
occupies 4 bytes of memory for both 32 and 64-bit architectures in Java.

7.Compiled and Interpreted:

Java compiler translates source code into byte code instruction and therefore
in second stage java interpreter generates machine code that can be directly
executed by the machine that is running in java programme.Interpreter is
called JVM(java virtual machine).

8.Multi Threading:

Java supports Multithreading. Multithreading means handling more than one


job at a time, so get more process get done in less time than it could with just
one thread.

9.Distributed:

Java was designed with the distributed environment. It has networking


facilities, so it can be transmit, run over internet. for ex. RMI and EJB are used
for creating distributed applications.

10.High performance:

JVM can execute byte codes (highly optimized) very fast with the help of Just
in time (JIT) compilation technique.

11. Re-usability of code:

Java provides the code reusability With the Help of Inheritance.

..........................................END......................

Q) Java virtual machine (JVM) ?


java provides both compiler and software machine called JVM for each
computer machine.The java compiler translates the java source code(java
programme) into an intermediate code known as byte code,which executes an
special type of machine,the machine is called java virtual machine(JVM) and
exists only inside the computer memory.The byte code is machine
independent code and can run in any system.Java interpreter takes the byte
code and translates it into its own system machine language and runs the
results.

..........................END...............

Q)Operators?

Operators are the constructs which can manipulate the values of the operands. Consider the
expression 2 + 3 = 5, here 2 and 3 are operands and + is called operator.

Java supports the following types of operators:

• Arithmetic Operators
• Assignment Operators
• Logical Operators
• Relational Operators
• Unary Operators
• Bitwise Operators
• Ternary Operators
• Shift Operators

1.Arithmetic Operators in Java:

• Arithmetic Operators are used to perform mathematical operations like addition,


subtraction, etc. Assume that A = 10 and B = 20 for the below table.

Operator Description Example


Adds values on either side of
+ Addition A+B=30
the operator
Subtracts the right-hand
– Subtraction operator with left-hand A-B=-10
operator
Multiplies values on either
* Multiplication A*B=200
side of the operator
Divides left hand operand
/ Division A/B=0
with right hand operator
Divides left hand operand by
% Modulus right hand operand and A%B=0
returns remainder

2.Assignment Operators in Java

An Assignment Operator is an operator used to assign a new value to a variable. Assume A


= 10 and B = 20 for the below table.

Operator Description Example


Assigns values from right side operands to left side
= c=a+b
operand
It adds right operand to the left operand and assigns
+= c += a
the result to left operand
It subtracts right operand from the left operand and
-= c -= a
assigns the result to left operand
It multiplies right operand with the left operand and
*= c *= a
assigns the result to left operand
It divides left operand with the right operand and
/= c /= a
assigns the result to left operand
It takes modulus using two operands and assigns the
%= c %= a
result to left operand
Performs exponential (power) calculation on operators
^= c ^= a
and assign value to the left operand

3.Relational Operators in Java

These operators compare the values on either side of them and decide the relation among
them. Assume A = 10 and B = 20.

Operator Description Example


If the values of two operands
== are equal, then the condition (A == B) is not true
becomes true.
If the values of two operands
!= are not equal, then condition (A != B) is true
becomes true.
If the value of the left operand
> (a > b) is not true
is greater than the value of
right operand, then condition
becomes true.
If the value of the left operand
is less than the value of right
< (a < b) is true
operand, then condition
becomes true.
If the value of the left operand
is greater than or equal to the
>= (a >= b) is not true
value of the right operand,
then condition becomes true.
If the value of the left operand
is less than or equal to the
<= (a <= b) is true
value of right operand, then
condition becomes true.

4.Logical Operators in Java

The following are the Logical operators present in Java:

Operator Description Example


&& (and) True if both the operands is true a<10 && a<20
|| (or) True if either of the operands is true a<10 || a<20
!(x<10 &&
! (not) True if an operand is false (complements the operand)
a<20)

5.Unary Operator in Java

Unary operators are the one that needs a single operand and are used to increment a
value, decrement or negate a value.

Operator Description Example


increments the value by 1. There is post-increment and pre-
++ a++ and ++a
increment operators
decrements the value by 1. There is post decrement and pre
— a– or –a
decrement operators
! invert a boolean value !a
6.Bitwise Operator in Java

Bitwise operations directly manipulate bits. In all computers, numbers are represented with
bits, a series of zeros and ones. In fact, pretty much everything in a computer is represented
by bits. Assume that A = 10 and B = 20 for the below table.

Operator Description Example


& (AND) returns bit by bit AND of input a&b
| (OR) returns OR of input values a|b
^ (XOR) returns XOR of input values a^b
~
returns the one’s complement. (all bits reversed) ~a
(Complement)

7.Ternary Operators in Java

The ternary operator is a conditional operator that decreases the length of code while
performing comparisons and conditionals. This method is an alternative for using if-else and
nested if-else statements. The order of execution for this operator is from left to right.

Syntax:

1 (Condition) ? (Statement1) : (Statement2);

• Condition: It is the expression to be evaluated which returns a boolean value.


• Statement 1: It is the statement to be executed if the condition results in a true state.
• Statement 2: It is the statement to be executed if the condition results in a false
state.

8.Shift Operators in Java


• Shift operators are used to shift the bits of a number left or right, thereby multiplying or
dividing the number. There are three different types of shift operators, namely left shift
operator()<<, signed right operator(>>) and unsigned right shift operator(>>>).
• Syntax:
• number shift_op number_of_places_to_shift;
..........................END.......................
Q) Priority of operators?
Operator precedence determines the grouping of terms in an expression. This affects
how an expression is evaluated. Certain operators have higher precedence than
others; for example, the multiplication operator has higher precedence than the
addition operator −
For example, x = 7 + 3 * 2; here x is assigned 13, not 20 because operator * has higher
precedence than +, so it first gets multiplied with 3 * 2 and then adds into 7.
Here, operators with the highest precedence appear at the top of the table, those with
the lowest appear at the bottom. Within an expression, higher precedence operators
will be evaluated first.
Category Operator Associativity
Postfix >() [] . (dot operator) Left toright
Unary >++ - - ! ~ Right to left
Multiplicative >* / Left to right
Additive >+ - Left to right
Shift >>> >>> << Left to right
Relational >> >= < <= Left to right
Equality >== != Left to right
Bitwise AND >& Left to right
Bitwise XOR >^ Left to right
Bitwise OR >| Left to right
Logical AND >&& Left to right
Logical OR >|| Left to right
Conditional ?: Right to left
Assignment >= += -= *= /= %= >>= <<= &= ^= |= Right to left

..........................................END.......................

Q)Decision(conditional) control statements?


Decision control statements depending on the result of evaluation of an
expression a statement or block of statements are executed. The following
statements are:

1.if statement 2.if else statement 3.nested if else statement 4.else if ladder

5.switch statement

1)if Statement: The if statement conditionally process the statements when


the specified condition is true.

Syntax:

if (condition)
{
Block of Statements
}
Next Statements
Flow chart:

Example:

class Test
{
public static void
main(String args[])
{
int x = 5;
if (x > 10)

System.out.println("Inside
If");

System.out.println("After if statement");
}
}
Output:

After if statement

ii)If..else statement: In if else statement if the condition is true ,then true block
statements are executed.Otherwise false block statements are executed.

Syntax:

if(condition)
{
true block statements
}
else
{
false block statements
}
next statements

flowchart:
Example:
class First
{
public static void main(String args[ ])
{
int a=5,b=4;
if(a>b)
System.out.println(a);
else
System.out.println(b);
}
}
Output:
5

iii)nested if else statement: The if else statement is placed with in another if else
statement such methods are called nested if else statements.
Syntax:
if(condition1)
{
if(condition2)
{
statements1
}
else
{
statements2
}
}
else
{
statements3
}
next statements

example:
class First
{
public static void main(String args[ ])
{
int a=5,b=4,c=6;
if(a>b)
{
if(a>c)
System.out.println(a);
else
System.out.println(c);
}
else
{
System.out.println(“zero”);
}
}
}
Output: 6
Q) parts of java ?
Ans:
Sun Microsystems Inc. has divided Java into 3 parts. They are 1. Java SE 2.
Java EE 3. Java ME. Let us discuss then in brief here.

Java SE

It is the Java Standard Edition that contains basic core Java classes. This
edition is used to develop standard applets and applications

Java EE

It is the Java Enterprise Edition and it contains classes they are beyond Java
SE. In fact, we need Java SE in order to use many of the classes in Java EE.
Java EE mainly concentrates on providing business solutions on a network.

Java ME

It stands for Java Micro Edition. Java ME is for developers who develop code
for portable device, such as a PDA or a cellular phone. Code on these devices
needs to be small in size and take less memory

……….END…..

Q)Java Naming Convention ?


Java naming convention is a rule to follow as you decide what to name your
identifiers such as class, package, variable, constant, method, etc.

But, it is not forced to follow. So, it is known as convention not rule. These
conventions are suggested by several Java communities such as Sun
Microsystems and Netscape.

All the classes, interfaces, packages, methods and fields of Java programming
language are given according to the Java naming convention. If you fail to
follow these conventions, it may generate confusion or erroneous code.
Advantage of Naming Conventions in Java:
By using standard Java naming conventions, you make your code easier to
read for yourself and other programmers. Readability of Java program is very
important. It indicates that less time is spent to figure out what the code does.

Naming Conventions of the Different Identifiers:


The following table shows the popular conventions used for the different
identifiers.

Identifiers Naming Rules Examples


Type

Class It should start with public class Employee

the uppercase {
letter. //code snippet
It should be a noun }
such as Color,
Button, System,
Thread, etc.
Use appropriate
words, instead of
acronyms.

Interface It should start with interface Printable

the uppercase {
letter. //code snippet
It should be an }
adjective such as
Runnable,
Remote,
ActionListener.
Use appropriate
words, instead of
acronyms.

Method It should start with class Employee


lowercase letter. {
It should be a verb // method
such as main(), void draw()

print(), println(). {
If the name //code snippet
contains multiple }
words, start it with }
a lowercase letter
followed by an
uppercase letter
such as
actionPerformed().

Variable It should start with class Employee


a lowercase letter {
such as id, name. // variable
It should not start int id;

with the special //code snippet


characters like & }
(ampersand), $
(dollar), _
(underscore).
If the name
contains multiple
words, start it with
the lowercase
letter followed by
an uppercase
letter such as
firstName,
lastName.
Avoid using one-
character variables
such as x, y, z.

Package It should be a //package


lowercase letter package com.javatpoint;

such as java, lang. class Employee


If the name {
contains multiple //code snippet
words, it should be }
separated by dots
(.) such as
java.util,
java.lang.

Constant It should be in class Employee


uppercase letters {
such as RED, //constant
YELLOW. static final
If the name int MIN_AGE = 18;

contains multiple
words, it should be //code snippet
separated by an }
underscore(_)
such as
MAX_PRIORITY.
It may contain
digits but not as
the first letter.

CamelCase in Java naming conventions:


Java follows camel-case syntax for naming the class, interface, method, and
variable.

If the name is combined with two words, the second word will start with
uppercase letter always such as actionPerformed(), firstName, ActionEvent,
ActionListener, etc.

…,……END……
Q)Data types?

• Data types are divided into two groups:

• Primitive data types -


includes byte, short, int, long, float, double, boolean a
nd char
• Non-primitive data types - such
as String, Arrays and Classes

Primitive Data Types:


A primitive data type specifies the size and type of variable values, and it has
no additional methods.
There are eight primitive data types in Java:

Data Type Size Description

byte 1 Stores whole numbers from -


byte 128 to 127

short 2 Stores whole numbers from -


bytes 32,768 to 32,767

int 4 Stores whole numbers from -


bytes 2,147,483,648 to 2,147,483,647

long 8 Stores whole numbers from -


bytes 9,223,372,036,854,775,808 to
9,223,372,036,854,775,807

float 4 Stores fractional numbers.


bytes Sufficient for storing 6 to 7
decimal digits

double 8 Stores fractional numbers.


bytes Sufficient for storing 15 decimal
digits

boolean 1 bit Stores true or false values


char 2 Stores a single character/letter
bytes or ASCII values

Data Type Default Value Default size

boolean false 1 bit

char '\u0000' (or)0 2 byte

byte 0 1 byte

short 0 2 byte

int 0 4 byte

long 0L 8 byte

float 0.0f 4 byte

double 0.0d 8 byte

a.Integer Datatype in Java:


int is used for storing integer values. Its size is 4 bytes and has a
default value of 0. The maximum values of integer is 2^31 and the
minimum value is -2^31. It can be used to store integer values unless
there is a need for storing numbers larger or smaller than the limits
Example- int a=56;
B Float Datatype in Java:
float is used for storing decimal values. Its default value is 0.0f and
has a size of 4 bytes. It has an infinite value range. However its always
advised to use float in place of double if there is a memory constraint.
Currency should also never be stored in float datatype. However it
has a single precision bit.

Example- float a=98.7f;

c. Character Datatype in Java:


char as the name suggests is useful for storing single value
characters. Its default value is ‘\u0000’ (or) 0 with the max value
being ‘\uffff’ (or) 65,535 inclusive and has a size of 2 bytes.

Example- char a=’D’;


It must be confusing for you to see this new kind of data ‘/u000’. This
is the unicode format which java uses inplace of ASCII.

d. Boolean Datatype in Dava:


boolean is a special datatype which can have only two values ‘true’
and ‘false’. It has a default value of ‘false’ and a size of 1 byte. It
comes in use for storing flag values.

Example- boolean flag=true;

e. Byte Datatype in Java:


It’s an 8 bit signed two’s complement . The range of values are -128 to
127. It is space efficient because it is smaller than integer datatype. It
can be a replacement for int datatype usage but it doesn’t have the
size range as the integer datatype. Its default value is 0.

Example- byte a = 10;

f. Short Datatype in Java:


This datatype is also similar to the integer datatype. However it’s 2
times smaller than the integer datatype. Its minimum range is -32,768
and maximum range is 32,767. It has a size of. Its default value is 0.
Example- short a= 54;

g. Long Datatype in Java:


This datatype primarily stores huge sized numeric data. It is a 64 bit
integer and ranges from -2^63 to +(2^63)-1. It has a size of 8 bytes and
is useful when you need to store data which is longer than int
datatype.its default value is 0L.

Example- long a= 1273762;

h. Double Datatype in Java:


This is similar to the float datatype. However it has one advantage
over float datatype i.e, it has two bit precision over the float datatype
which has one bit precision. However it still shouldnt be used for
precision sensitive data such as currency. It has a range of -2^31 to
(2^31)-1. Its default value is 0.0d.

Example- double DataFlair=99.987d;

. Non-primitive Data Types in Java:


These are the datatypes which have instances like objects. Hence they are
called reference variables. They are primarily classes, arrays, strings or
interfaces.
………..END…

Q) constants (or) literals?


A value which is fixed and does not change during the execution of a
program is called constants in java. In other words, Java constants are
fixed (known as immutable) data values that cannot be changed.
Java supports various types of constants. They are as follows:
1. Integer Constants
2. Real Constants
3. Character Constants
4. String Constants
Integer Constants in Java

An integer constant is a sequence of digits without a decimal point. For


example, 10 and -200 are integer constants. There are three types of
integer constants. They are as follows:
1. Decimal integer
2. Octal integer
3. Hexadecimal integer

Decimal Integer:

Decimal integer is a sequence of digits from 0 to 9. These constants are


either positive or negative. Plus sign is optional.
For example, 24, -55, 0, and +3425 are valid examples of decimal integer
constants in decimal notation.
Non-digit characters, embedded spaces, and commas are not allowed
between digits. For example, 12 3450, 20.00, $1200 are illegal numbers.

Octal Integer:

An octal integer constant is a sequence of any combination of digits


from 0 to 7. Octal integer always starts with 0. The digits 8 and 9 are not
valid in octal notation.
The examples of valid integer constants in octal notation are 024, 0,
0578, and 0123.

Hexadecimal Integer:

Hexadecimal integer constants consist of digits 0 to 9 and alphabets “a”


to “f” or “A” to “F”. These constants are preceded by 0x. Letter “A” to “F”
represents the numbers 10 to 15.
The valid examples of hexadecimal integer constants are 0x23, 0x5B,
0x9F, 0x, etc. “x” can also be either small or capital.
We rarely use octal and hexadecimal integer numbers system in
programming.

Real (Floating-point) Constants in Java

Real constants consist of a sequence of digits with fractional parts or


decimal points. These constants are also called floating-point constants.
The valid examples of real constants are 2.3, 0.0034, -0.75, 56.7, etc.
These numbers are shown in the decimal point notation. It is also
possible that the number may not have digits before decimal point or
digits after the decimal point. For example, 205., .45, -.30, etc.

A real constants number can also be expressed in exponential or


scientific notation. For example, the value 235.569 can also be written as
2.35569e2 in exponential notation. Here, e2 means multiply by 10^2.
It has the following general form:
mantissa e exponent

Where,
mantissa is either integer number or a real number expressed in decimal
notation. The term exponent is an integer with an optional plus or minus
sign.
The letter “e” is used to separate mantissa and exponent which can be
written in either lowercase or uppercase. The valid examples of floating
points constants are 0.54e2, 12e-4, 1.5e+5, 2.13E4, -1.5E-4, etc.
Embedded (blank) space is not permitted in any numeric constants.
Exponential notation is useful to represent either very small or very large
number in magnitude.
For example, 250000000 can also be written as 2.5e8 or 25E7. Similarly, -
0.000000021 is equivalent to -2.1e-8.

Single Character Constants in Java

A single character constant ( or simply character constant) is a single


character enclosed within a pair of single quote. The example of single-
character constants are as follows:
‘5’ ‘x’ ‘;’ ‘ ‘ etc.
The last constant is blank space. The character constant ‘5’ is not
equivalent to the number 5.

String Constants

A string constant is a sequence of characters within a pair of double-


quotes. The characters can be alphabets, special characters, digits, and
blank spaces. The valid examples of string constants are given below:
“Hello Java” “1924” “?…!” “2+7” “X” etc.

Backslash Character Constants

Java provides some special backslash character constants that are used
in output methods. For example, the symbol ‘n’ which stands for newline
character.
There are various types of backslash character constants that are
supported by java. The list is given in the below table.
Table: Backslash Character Constants
Constants Meaning

‘b‘ back space

‘f‘ form feed

‘n‘ new line

‘r‘ carriage return

‘t‘ horizontal tab

‘‘‘ single quote

‘”‘ double quote

‘ ‘ backslash

……..END…..

You might also like