Java Tutorial 2
Java Tutorial 2
Ans-Data types in Java are of different sizes and values that can be stored in the
variable that is made as per convenience and circumstances to cover up all test cases.
Java has two categories in which data types are segregated
It is a kind of data structure that stores the It is a type of data structure that can store the
data of only one type. data of more than one type.
The size depends on the type of the data In case of non-primitive data structure, size is not
structure. fixed.
It can be used to call the methods. It cannot be used to call the methods.
Boolean data type represents either true or false which is intended to represent the
two truth values of Boolean algebra, but the size of the boolean data type is virtual
machine-dependent. Values of type boolean are not converted implicitly or
explicitly (with casts) to any other type. But the programmer can easily write
conversion code.
The byte data type is an 8-bit signed two’s complement integer. The byte data type is
useful for saving memory in large arrays.
1. Strings
Strings are defined as an array of characters. The difference between a character
array and a string in Java is, that the string is designed to hold a sequence of
characters in a single variable whereas, a character array is a collection of separate
char-type entities. Unlike C/C++, Java strings are not terminated with a null
character.
2. Class
A class is a user-defined blueprint from which objects are created. It represents the
set of properties or methods that are common to all objects of one type. In general,
class declarations can include these components, in order:
1. Modifiers: A class can be public or has default access.
2. Class name: The name should begin with an initial letter
3. Superclass(if any): The name of the class’s parent (superclass), if any,
preceded by the keyword extends. A class can only extend (subclass) one
parent.
4. Interfaces(if any): A comma-separated list of interfaces implemented by
the class, if any, preceded by the keyword implements. A class can
implement more than one interface.
5. Body: The class body is surrounded by braces, { }.
JAVA TUTORIAL 2ND
3. Interface
Like a class, an interface can have methods and variables, but the methods declared
in an interface are by default abstract
Interfaces specify what a class must. It is the blueprint of the class.
An Interface is about capabilities like a Player may be an interface and any
class implementing Player must be able to (or must implement) move().
So it specifies a set of methods that the class has to implement.
If a class implements an interface and does not provide method bodies for
all functions specified in the interface, then the class must be declared
abstract.
A Java library example is Comparator Interface. If a class implements this
interface, then it can be used to sort a collection.
4. Array
An Array is a group of like-typed variables that are referred to by a common name.
Arrays in Java work differently than they do in C/C++. The following are some
important points about Java arrays.
In Java, all arrays are dynamically allocated. (discussed below)
Since arrays are objects in Java, we can find their length using member
length. This is different from C/C++ where we find length using size.
A Java array variable can also be declared like other variables with [] after
the data type.
The variables in the array are ordered and each has an index beginning
with 0.
Java array can also be used as a static field, a local variable, or a method
parameter.
The size of an array must be specified by an int value and not long or
short.
The direct superclass of an array type is Object.
Every array type implements the
interfaces Cloneable and java.io.Serializable .
The import statement can be used to import an entire package or sometimes import certain
classes and interfaces inside the package. The import statement is written before the class
definition and after the package statement(if there is any). Also, the import statement is
optional.
A program that demonstrates this in Java is given as follows:
Example
Live Demo
import java.util.LinkedList;
JAVA TUTORIAL 2ND
public class Demo {
public static void main(String[] args) {
LinkedList<String> l = new LinkedList<String>();
l.add("Apple");
l.add("Mango");
l.add("Cherry");
l.add("Orange");
l.add("Pear");
System.out.println("The LinkedList is: " + l);
}
}
Output
The LinkedList is: [Apple, Mango, Cherry, Orange, Pear]
The Java compiler breaks the line of code into text (words) is called Java tokens
A token is the smallest element of a program that is meaningful to the compiler.
Tokens can be classified as follows:
1. Keywords
2. Identifiers
3. Constants
4. Special Symbols
5. Operators
1. Keyword: Keywords are pre-defined or reserved words in a
programming language. Each keyword is meant to perform a
specific function in a program. Since keywords are referred names
for a compiler, they can’t be used as variable names because by
doing so, we are trying to assign a new meaning to the keyword
which is not allowed. Java language supports following keywords:
2 Identifiers: Identifiers are used as the general terminology for naming of variables,
functions and arrays. These are user-defined names consisting of an arbitrarily long
sequence of letters and digits with either a letter or the underscore(_) as a first
character. Identifier names must differ in spelling and case from any keywords. You
cannot use keywords as identifiers; they are reserved for special use. Once declared,
you can use the identifier in later program statements to refer to the associated value.
A special kind of identifier, called a statement label, can be used in goto statements.
Examples of valid identifiers :
JAVA TUTORIAL 2ND
3 Constants/Literals: Constants are also like normal variables. But, the only
difference is, their values can not be modified by the program once they are defined.
Constants refer to fixed values. They are also called as literals.
Constants may belong to any of the data type.
4 Special Symbols: The following special symbols are used in Java having some
special meaning and thus, cannot be used for some other purpose.
[] () {}, ; * =
Brackets[]: Opening and closing brackets are used as array element
reference. These indicate single and multidimensional subscripts.
Parentheses(): These special symbols are used to indicate function calls
and function parameters.
Braces{}: These opening and ending curly braces marks the start and end
of a block of code containing more than one executable statement.
comma (, ): It is used to separate more than one statements like for
separating parameters in function calls.
semi colon : It is an operator that essentially invokes something called an
initialization list.
asterick (*): It is used to create pointer variable.
assignment operator: It is used to assign values.
5 Discuss various loop statements and branching statements available in Java? Show their syntax.
BRANCHING STATEMENT
Break And Continue Statements Are Known As Branching Statements Or Jump Statements.
These Statements Can Be Used Inside Any Loop(For,Do-While, While).
These Statements Are Used To Skip Some Statements Or Immediately Terminate The Loop
When The Condition Is Satisfied.
Break-
Break Statement Terminates From The Loop Immediately. When The Compiler Detects
The Break Statement It Stops The Iteration Of The Loop There And Returns To The First
Statement After The Loop.
Syntax:-
Suppose We Have Numbers From 1-20 And We Want To Print The First 10 Numbers. So, We Can Do
This By Using Break Statement.
Continue Statement
Continue Statement Is Used To Skip The Iteration Of The Loop.
For Example, If We Want To Print Numbers From 1-20 But We Don’t Want To Print A
Particular Number Say 11. This Can Be Done Either By Looping Twice Or By Using The
Continue Statement.
o Arithmetic Operator,
o Relational Operator,
o Bitwise Operator,
o Logical Operator,
o Assignment Operator.
1 Java arithmetic operators are used to perform addition, subtraction, multiplication, and
division. They act as basic mathematical operations.
2 Java assignment operator is one of the most common operators. It is used to assign the
value on its right to the operand on its left.
3Relational Operators in Java are used to comparing two variables for equality, non-equality,
greater than, less than, etc. Java relational operator always returns a boolean value - true or
false.
4A bitwise operator in Java is a symbol/notation that performs a specified operation on standalone
bits, taken one at a time. It is used to manipulate individual bits of a binary number and can be used
with a variety of integer types – char, int, long, short, byte.
5 Logical operators can be defined as a type of operators that help us to combine multiple conditional
statements. There are three types of logical operators in Java: AND, OR and NOT operators.
AND operator returns true when both conditions under evaluation are true, otherwise it returns
false. Tt=t
OR operator returns true if any one of the given conditions is true. OR operator returns false if and
only if both conditions under evaluation are false. Tf=t ff=f
NOT operator accepts a single value as an input and returns the inverse of the same. This is a unary
operator unlike the AND and OR operators.
Class Object
Q.8 How do you swap two numbers without using a third variable in Java?
X= 25 (First number), Y= 23 (second number)
Swapping Logic:
X = X + Y = 25 +23 = 48
Y = X - Y = 48 - 23 = 25
X = X -Y = 48 - 25 = 23
and the numbers are swapped as X =23 and Y =25.
Algorithm
o STEP 1: START
o STEP 2: ENTER x, y
o STEP 3: PRINT x, y
o STEP 4: x = x + y
o STEP 5: y= x - y
o STEP 6: x =x - y
o STEP 7: PRINT x, y
o STEP 8: END