Class XI Java Fundamental
Class XI Java Fundamental
Character Set : The character set is a set of alphabets, letters and some
Special characters that are valid in Java language.
Java actually uses Unicode, which includes ASCII and other characters from languages around
the world.
ASCII Character Set: ASCII stands for “American Standard Code for Information
Interchange”. It is a 7-bit coding system that allows 27 = 128 different characters.
The Extended ASCII coding system uses 8-bit character set that allows 28 =256 different
characters.
Unicode Character Set: It is an International character set designed to represent all the
characters found in languages around the world. It can use between 8 to 32 bits to represent
characters. Essentially, first 128 characters in the Unicode character set are same as of the
ASCII character set.
Tokens : A token is the smallest individual unit of a program that is meaningful to the
compiler.
Types of Tokens
The various types of tokens available in Java are:
1. Identifiers: Identifiers are the names given to different parts (class name, variable
name etc.) of the program.
Naming Rules for an identifier
• It may contain alphabets, digits and 2 special characters dollar ($) and underscore ( _ ).
• It should not start with a digit.
• It should not have a reserved/key word.
e.g.
Valid identifiers: Roll_no , AB3 , sub_1 are valid
Invalid Identifiers: 3X , for , Rollno. , Roll no are invalid
2. Keywords: These are the reserved words which carry a special meaning to language
compiler.
e.g. for, return, class, void, public, int etc.
3. Literals: These are the tokens that never change their values during execution of the
program.
Types of literal(constants)
Integer literals: 3,10 etc.
Floating(Real) literals : 5.5 , 2.0 etc.
String literals : “Hello world”, “a” etc.
Character literals : ‘a’, ‘\n’ etc.
Boolean literals : true and false.
Null litral : null,’\0’
5. Punctuators/Separators: These are the symbols and special characters that make
different parts of code separate from each other.
e.g. : [ ] , { } , ( ) , ; etc.
Escape Sequences: Escape characters (also called escape sequences ) in general are used to
signal an alternative interpretation of a series of characters. In Java, a character preceded by a
backslash (\) is an escape sequence and has special meaning to the java compiler.
e.g. \n , \t etc.
Data Types: Data types are means to identify the type of data and associated operations for
handling it. In java, there are two types of data types:
1. Primitive/fundamental data types: These are pre-defined data types and are an integral
part of the Java programming language.
There are 8 primitive data types in Java:
Data type Type Size (in Byte) Default Value
Integers byte 1 0
short 2 0
int 4 0
long 8 0
Real Numbers float 4 0.0f
double 8 0.0d
Character char 2 ‘\u0000’
Boolean boolean 1 false
2. Non-Primitive / Derived data types: These are composite data types which are composed
of fundamental data types.
e.g. Array, Class , String etc.
Variables: A Variable is a named storage locations which holds a value. A variable name should
abide by the rules of naming an identifier.
Declaration of Variable: The general syntax to declare a variable is:
<data type> <variable name>;
e.g. int roll;
double marks;
Static initialization: Initializing a variable before the execution of a program is called static
initialization.
e.g. int roll=10;
Dynamic initialization: Initializing a variable during the execution of a program is called
dynamic initialization.
e.g. sum = a+b;
Symbolic constant: A symbolic constant is a variable whose value does not change during the
execution of the program. We can make a variable as symbolic constant with “final” keyword.
e.g. final int a=10;
Comments in Java: Comments are used to understand the logic and flow of a program.
In Java, there are 3 types of comments:
1. Single line comment ( // ) : It is the simplest comment in Java that appears in single line.
This type of comment begins with double forward slash (//).
2. Multi-line comment ( /*….*/ ) : Multi-line comment extends over multiple lines. This type
of comment starts with /* and ends with */ . There must be no space between slash and
asterisk.
3. Documentation comment ( /** …….. */ ): A special comment looks like multi-line comment.
But it is used to generate external documentation about the source code. This type of
comment starts with /** and ends with */ .
e.g.
/* Java program
* depicting comments
* This is multiline comment Multi-line comment
*/
public class comments
{
// The main () method declaration Single line comment
public static void main(String args[])
{
int a=10; //declaration and initialization of the variable ‘a’
}
}
Classes in Java
Object: An identifiable entity that has characteristics and behavior is called an object.
Features of an Object:
1. Identity: uniquely identify an object.
e.g. Nikunj Nikunj Nikunj
2. Attribute: Characteristics of
an object.
e.g. Class, Roll No., age etc.
3. State: Depicted through the
values of its attributes.
e.g. 11B,12B,25 etc.
4. Behavior: Functionalities associated with an object.
e.g. Promotion etc.
Class: A collection of objects sharing common properties and behaviours.
or
A class is a way to wrap data (characteristics) and its associated functions (behaviours) into a
single unit.
e.g.
public class xyz
{
int a; // Data member (represent characteristics)
public void func() // Member function (represents behavior)
{
.
.
.
}
}
Types of Variables:
• Instance variables: Global variables declared outside all the functions.
• Class variables: Global variables, preceded with ‘static’ keyword, declared outside all
the functions.
• Argument variables: Function’s parameter list.
• Local variables: Declared inside the body of a function.
e.g.
public class variables
{
int a; // instance(object) variable
static int b; // Class variable
public static void main (int x)
{ // Argument variable
int y; // Local Variable
.
.
}
}
Instantiation: Creating an object with the help of ‘new’ operator is called instantiating an
object.
The General Syntax to instantiating an object is as:
<Class-name> <Object-name> = new <Class-name> ( Argument(s));
1. The static member allocates a memory 1. The non-static member creates its
location only once and same memory copies on the basis of number of objects
location is shared by all objects of the created for the class .
class.
2. Static members can be accessed 2. Instance members are accessible
without instance. Name of the class is used through an object only.
to access the static members.
Note: A static members cannot access a non-static members of the class directly.
An object of the class is required to access non-static member by a static member.
public class XYZ
{
int a; // instance(object) variable
static int b; // Class variable
public void function()
{
int s=a+b;
System.out.println(“sum= ” +s);
}
public static void main ()
{ XYZ ob = new XYZ ();
b=15; //Accessing a static variable
ob.a = 10; //Accessing an instance variable
ob.function(); //Calling an instance method
} }
Q.
Sol.
import java.util.*;
class Merger
{
long n1,n2,mergNum;
//default Constructor
public Merger()
{
n1=0;
n2=0;
mergNum=0;
}