Java Fundamentals
Java Fundamentals
CHARACTER SET
• The character set is a set of alphabets, letters
and some special characters that are valid in
Java language.
• 2+fact(5)
• A+(b*2)
ARITHMETIC OPERATORS
• Arithmetic operators are used to perform common
mathematical operations.
• The standard arithmetic operators are :
addition ( + ), subtraction ( - ), multiplication ( * ),
division ( / ) and Modulus (%)
RELATIONAL OPERATORS
Java has six relational operators that compare two
numbers and return a boolean value.
The relational operators are < , > , <= , >= , == , and !=
LOGICAL OPERATORS
• A logical operator is a symbol or word used to connect two
or more expressions.
• It returns a Boolean result that is based on the Boolean
result of one or two other expressions.
• Common logical operators include AND, OR, and NOT.
ASSIGNMENT OPERATORS
Assignment operators are used in Java to assign values to variables.
• “=”: This is the simplest assignment operator which is used to assign
the value on the right to the variable on the left. a = 10; ch = 'y';
Compound Statement or shorthand notation Assignment operators
TERNARY OPERATORS (CONDITIONAL OPERATOR ?: )
• The ternary operator is a part of Java’s conditional
statements. As the name ternary suggests, it is the only
operator in Java consisting of three operands.
• The ternary operator can be thought of as a simplified
version of the if-else statement with a value to be returned.
DATA TYPES
• Data type defines the values that a variable can take.
• For example, if a variable has int data type, it can only take
integer values. int a;
PRIMITIVE DATA TYPE
• Predefined
• primitive types are the basic types
(fundamental) of data.
• byte, short, int, long, float, double, boolean,
char.
• primitive variables store primitive values
REFERENCE DATA TYPE
• Created by the user.
• Reference datatypes in java are those which
contains reference/address of dynamically
created objects.
• reference types are any instantiable class as
well as arrays.
• reference variables store addresses.
VARIABLE
• A variable is a location in memory (storage
area) to hold data.
• To indicate the storage area, each variable
should be given a unique name (identifier).
• Here's an example to declare a variable in Java.
int speed = 80;
80
int a;
Rvalue a
Speed
4 bytes
4 bytes
Lvalue
CONSTANTS
• The final modifier means that the variable's
value cannot change. Once the value is
assigned, it cannot be reassigned.
Example:
final int i = 10 ;
i = 30 ; // Error because i is final.
NEW OPERATOR
The new operator is used in Java to create a new
object of a class.
class prg
{ int a, b;
public static void main()
{ prg obj=new prg();
}
}
It can also be used to create an array object.
int [] arr = new int[] {5,6,11,3,8};
int [] arr = new int[10];
int x=6; int [] arr = new int[x];
TYPE CONVERSION
Type Conversion refers to changing an entity of one
datatype into another.
There are two types of conversion:
• Implicit Conversion
• Explicit Conversion
IMPLICIT TYPE CONVERSION
• It is also known as Type Promotion or Automatic
Type Conversion.
• Implicit conversion takes place when two data
types are automatically converted. This happens
when:
The two data types are compatible.
When we assign value of a smaller data type to a bigger
data type.
int val1 = 11;
int val2 = 35; Compiler will automatically convert the
char ch=‘a’;
float sum; sum of 2 integer variable into float and
int x=ch;
sum = val1 + val2; will store the result in float type
variable sum
EXPLICIT TYPE CONVERSION
• It is also known as type casting.
• These conversions are done explicitly by users using the
pre-defined functions type().
• This is useful for incompatible data types where automatic
conversion cannot be done.
char ch = 'c';
int num = 88;
ch =(char) num;
float x=4.5F; int y=(int)x;
int a=5, b=2;
float c=a/b; //c=2.0
float c=(float) a/b; //c=2.5
EXPRESSIONS
An expression is a construct made up of variables,
operators, and method invocations, which are
constructed according to the syntax of the language,
that evaluates to a single value.
OPERATOR PRECEDENCE (PEMDAS)
JAVA STATEMENTS
A sentence forms a complete idea which can
include one or more clauses. Likewise,
a statement in Java forms a complete command
to be executed and can include one or more
expressions.
CLASS (STATIC) VARIABLES AND INSTANCE VARIABLES
Instance variables are declared in a Class variables also known as static
class, but outside a method, variables are declared with the static
constructor or any block. keyword in a class, but outside a
method, constructor or a block.
Instance variables are created when an Static variables are created when the
object is created with the use of the program starts and destroyed when
keyword 'new' and destroyed when the the program stops.
object is destroyed.