[go: up one dir, main page]

0% found this document useful (0 votes)
34 views7 pages

Basics Theory

The document discusses key concepts in Java including character sets, tokens, keywords, identifiers, literals, operators, variables, data types, expressions, and type conversion. It provides definitions and examples of each concept.

Uploaded by

zemo kumar
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)
34 views7 pages

Basics Theory

The document discusses key concepts in Java including character sets, tokens, keywords, identifiers, literals, operators, variables, data types, expressions, and type conversion. It provides definitions and examples of each concept.

Uploaded by

zemo kumar
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/ 7

Java Basics

Java Character Set.- Character set is a collection of all the characters that can
be used in a Java program. A character represents any letter (whether capital or small),
digits (0 to 9) or any symbol (%, &, ^, @, $, |, etc). java uses Unicode character set. It
occupies 2 byte to store a character.It can recognize character of most of the language
used in the world.
Token. - Token is the smallest fundamental unit in a program. They are the valid
characters or symbols that can be used in a Java program.The various type of token
available in java is
i. Literals ii. Identifier iii. Keywords. iv puncturators v. operators

Keywords - Keywords are reserved words in Java and thus provide a special
meaning to the Java compiler. Example of keywords are : class, int, for, if etc.

Identifiers - Identifiers are the name given to different parts of the programs e.g.
function names, variable names, class or object names etc.
RULES OF NAMING IDENTIFIERS
It can have any alphabet (capital or small), digits, underscore and dollar sign
characters.
It should not begin with digits or should not contain any special character except(_ , $).
It cannot have a space in between.
It must not be a keyword.

Literals(Constant) - Literals are fixed data value which can not change during
execution of progs. there are 6 types of literals in java.
1. INTEGER LITERALS :- an integer literal are whole numbers have at least
one digit and must not contain and decimal point. It may contain either + or – sign. 43,
0, -76 are integers
2. FLOATING LITERAL: - floating literals are numbers having fractional parts .it
may be + or -. These may be written in one of the two forms fractional form or the
exponent forms. -5.93, 58.0, 1.08, -0.77
3. BOOLEAN LITERALS: - the Boolean type has two values represented by the
literal true and false.
4. CHARACTER LITERALS :- A character literals in java must contain one
character and must be enclosed in single quotation marks. ‘A’, ‘5’, ‘&’ are characters.
5. STRING LITERALs :-is a sequence of zero of more characters surrounded by
double quotes. ”basic”, “abc”, “88”, “@” are strings.
6. NULL LITERALS :- The Null literal (written as null ) represents the one and only
value of the null type

Punctuator -
punctuators are also called Separators. These are symbols and special characters that
make different parts of code separate from each others.

Punctuators used in java program are:- , (comma), ; (semicolon), . (period), (,) (group
of parenthesis), {,} (Pair of braces), [,] (pair of square brackets).

Operator - operator are the symbol which tell the computer which operation to take
place on operands
Operand : - are the data items on which operation takes place.

Category of Operator :-
Unary operator :- uses only one operand(++, --, !,unary(+ , -)
Binary operator :- uses two operand(+, *, /, &&)
Ternay operator :-uses three operand(?:)

TYPE OF OPERATOR :-
• Arithmetical Operator
• Relational Operator
• Logical Operator
• Assignment Operator
• Ternary Operator
ARITHMATICAL OPERATOR: - the operators which are applied to perform
arithmetical calculation in a program are known as arithmetical operator. For example
+, -, *, /, %
%(modulus) is used to find reminder of two operand.(5%2 is 1)
INCREMENT/DECREMENT OPERATOR: - they are used to increment/decrement the
value of a integer variable by 1
x++, ++x will increment value of x by 1
x--, --x will decrement value of x by 1
POSTFIX :- evaluates to the value of the operand after the increment /decrement
operation
e.g if x=5 initially then y=x++; will store 5 into y and x will become 6.
if x=5 initially then y=x--; will store 5 into y and x will become 4.
PREFIX :- evaluates to the value of the operand before the increment /decrement
operation
e.g if x=5 initially then y=++x; will store 6 into y and x will become 6.
if x=5 initially then y=--x; will store 4 into y and x will become 4.

RELATIONAL OPERATOR :- are used to show relationship among the operands.


Relational operators compare the values of the varibles and results in terms of true or
false. Eg:- > ,<, >=,<=,!=, ==
LOGICAL OPERATOR :- are used to combine two or more conditions. &&, ||, !
&&(AND) evaluates the condition as true when all conditions combined with it is true.
||(OR) evaluates the condition as true when any on of the conditions combined with it
is true.
! (NOT ) is used to invert the result of the condition

ASSIGNMENT OPERATOR :- = is assignment operator used to assign value to given


variable.(a=5 ,b=a+10)

TERNARY OPERATOR :- deal with three operands. It also called condition assignment
statement because the value assigned to a variable depends upon a logical expression.

Syntax: variable= (test expression) ? expression 1: expression 2;


Ex. z=(x>y)?x:y;
Here value of z will be x if condition is true otherwise z will be y.
Ex. boolean flag=age>18? true:false
Here value of variable flag will be true if age is greater than 18 otherwise flag will
become false.

SHORTHAND OPERATOR :- java provides shorthand operators to combine the


arithmetic and assignment operator into a single operator .(+=, -=, *=, /=, %=)
a+=b means a=a+b, a/=5 means a=a/5

Operator Precedence - determines the order in which expressions are evaluated.


It is a set of rules that establishes which operators get evaluated first when more than
one operator is present in the expression
Arithmetic operator precedence follow (BEDMAS) rule.
Logical operator precedence (NOT, AND, OR)
consider the following example
x = 5 + 4 * 6;
The value of this expression is 29. Multiplication has been performed first in this
expression then addition.

Operator Associatively - rules determine the grouping of operands and operator


in and expression with more than one operator of the same precedence.

NON GRAPHIC CHARACTER - are those character which can not be directly type
from the keyboard. E.g. backspace, tabs, carriage return etc.

Escape Sequence - non graphic character can be represented by using escape


sequence. An escape sequence is represented by a backslash (\) followed by one or
more characters. E.g. \b backspace, \a alert bell, \n new line, \t horizontal tab,
\\ backslash, \? question mark, \’ single quote, \” double quote.

Variable - A variable is a named memory location, which contains a value. The


value of variable can be changed depending upon the circumstances and problem in the
program.
Data Type - Data types are means to identify the type of data and associated
operation of handling it.there are two type of data type. 1: - Primitive type 2: - Non –
primitive (Reference) type.
1. PRIMITIVE DATE TYPE - The type of data which are independent of any
other type, are known as Primitive data types. These types are known as fundamental
or basic data type. They are pre-defined or built in data type. E.g. byte, short, int, long,
char, float, double, boolean.
2. REFRENCE DATA TYPE - they are composed from primitive data type e.g.
array, class etc.
The general format of variable declaration is as follows:
<data-type><variable-name>;
INTEGER VARIABLE - A variable declared integer type contains a whole number.
integer variables are 4 types(byte, short, int, long) .
int age;
int a,b,c;
long x;

Data type Size Range Default value


Byte 1 byte -128 to 127 0
Short 2 byte -32768 to 32767 0
31 31
Int 4 byte -2 to 2 -1 0
63 63
Long 8 byte -2 to 2 -1 0L
Note - By default java treats whole numbers as int data type.

FLOATING VARIABLE :- a variable to be floating type if we want to store real


numbers in it.float variables are two types(float, double)
float per;
double radius,area;

Data type Size Range Default value


float 4 byte 3.4 x10-38 to 3.4 x1038 0.0F
double 8 byte 1.7 x 10-308 to 1.7 x 10308 0.0
Note; - By default java treats fractional numbers as of double data type.

CHARACTER VARIABLE :- A character type variable contains a single character. It


occupies 2 bytes. It can store 65536 characters. It default value is null..
char ch;
BOOLEAN VARIABLE :- it is special type in which a variable contains a constant as
true of false these are nonfigurative constant. It occupies 1 byte in memory.
boolean a;
STRING VARIABLE :-string is reference (non primitive) data type. it is used to store
set of characters.
String name;

Expression - expression is any valid combination of operators, constants and


variable.
Math expression H=ut+1/2at2
Java expression H=u*t+1/2*a*t*t
PURE EXPRESSION : - all the operands are of same data type.
Example : a+b if a and is same type then it is pure expression
MIXED EXPRESSION : - the operands are of mixed or different data type.
Example : a+b if a and b are different type then it is mixed expression.

Type Conversion - process of converting one predefined type into another is called
Type Conversion. It is used in mixed expression where result can be obtained by
converting the various data type into the single type. Two types of type conversion.

1. IMPLICT TYPE CONVERSION :- the data type of the result get converted
automatically into its higher type without intervention of the user. This system of type
conversion is known as implicit type conversion or coercion .
2. EXPLICIT TYPE CONVERSION:- in this type conversion the data type gets
converted to another type of depending upon choice. This means the user forces the
system to get back into the desired data type also called type casting.
Syntax : (type)expression
e.g. int a,b;
float x=(float) (a+b);

Variable scope - generally refers to the program region in which a variable is


accessible.

Compound statement (Block) - Compound statement is a set of two of more


statements enclosed with in { }. it is also called a block.
{
t=x;
x=y;
y=x;
}

final Keyword - This keyword is used while declaring a variable. These variables
are initialised during the declaration and their values cannot be modified further in the
program. This way keyword final makes the variable as constants.
Example:- final double pi=3.14;

DIFFERENCE BETWEEN print() and println() :- print(0 displays the given


data/information but do not change line after display where as println() displays the
given data/information and changes line after displaying it.
Name two specific purpose of + operator.
The + operator is used for arithmetic addition and string concatenation.
e.g. System.out.print(“Computer “);
System.out.println(“Application”);
System.out.println(“Class X”);
Output:- Computer Application
Class X
DIFFERENCE BETWEEN OPERATOR AND EXPRESSION. An operator is a symbol
that performs a specific operation on the operands. An operator can be unary operator
and binary operator depending on number of operands it operates upon.
e.g +,-,*,/, % are binary operator.
An expression is formed by the combination of operator of operators with operands. E.g
a+b/2 * c is and arithmetic expression. [2005]

DIFERENCE BETWEEN = AND == :- The ‘=’ (assignment operator) is to assign the


value of the variables, whereas ‘==’ (relational operator) is to compare two values. For
example.
int x=95; //assignment
int y=102; //assignment
if(x==y) //condition check

Difference between unary and binary operator


Unary Operator Binary Operator
i. Operators that act on on operand are I Operator which require two operands for
reffered to as unary operator. their functing are calles binary operator.

ii. ++,--, unary + or unary – are unary ii. Arithmatic operator, Relational
operator operator are Binary operator.

Difference between Primitive data type and Refrence Data Type.


Primitive Data Type Refrence Data Type
i. These are the basic of fundamental 1. These data types are constructed from
data type supported by java. primitive data types.
ii. The primitive data type variables store ii. The reference data type variables
the actual value and their operations store the addresses or the references of
deal with rvalue. the memory location of the actual data.
iii. example int, char , float, double These deal with the lvalue.
iv. use of new operator is not necessary iii. Examples:- class, array, refrences.
iv. Use of new operator is must.

Similarity and Difference between java character set (Unicode) and ASCII
code.
Similarity:- The first 128 characters in the Unicode character set are same as ASCII
character set. i.e. they represent the same characters.
Difference:-
Unicode ASCII code
i. It is a 2 byte character code set i. it is a 7 bit character code set.
ii. It can represent 65536 characters.
ii. ii. It can represent only 128
characters.

Difference between Implicit and Explicit type conversion.


Implicit type conversion Explicit type conversion
1. This type of conversion is performaed 1. This type of conversion is forces by the
by compiler user intervension not user.
required. 2.keyword data type is used to force
2. No keyword is requied to perform this explicit type conversion.
conversion. Ex. double x=12.5;
int x=12; int i=(int)x;
double d=x;

Difference between rvalue and lvalue.


Rvalue is called real value. It referes to data item stored in the variable.
Lvalues is calles location value it refers to the memory address where variable is
stored.
Example :
Int a=5;
Rvalue of a is 5. Lvalue of a is memory address of a.

You might also like