0% found this document useful (0 votes)
14 views34 pages

Java Fundamentals

The document discusses Java fundamentals including character sets, tokens, data types, variables, operators, expressions, and statements. Key topics covered are keywords, identifiers, literals, punctuators, unary and binary operators, primitive and reference data types, and implicit and explicit type conversion.

Uploaded by

RISHABH YADAV
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
14 views34 pages

Java Fundamentals

The document discusses Java fundamentals including character sets, tokens, data types, variables, operators, expressions, and statements. Key topics covered are keywords, identifiers, literals, punctuators, unary and binary operators, primitive and reference data types, and implicit and explicit type conversion.

Uploaded by

RISHABH YADAV
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 34

JAVA FUNDAMENTALS

CHARACTER SET
• The character set is a set of alphabets, letters
and some special characters that are valid in
Java language.

• The smallest unit of Java language is the


characters need to write java tokens.

• These character set are defined by Unicode


character set.
DIFFRENCE BETWEEN UNICODE AND ASCII CODE
(ASCII and Unicode are two character encodings.)

• ASCII originally used seven bits to encode each


character. Unicode uses a variable bit encoding
program where you can choose between 32, 16,
and 8-bit encodings.

• Unicode is standardized while ASCII is not.

• Unicode represents most written languages in


the world while ASCII does not.
TOKENS
Java tokens are smallest elements of a program which are
identified by the compiler. Tokens in java include keywords,
identifiers, literals, operators and, separators.
KEYWORDS
• Keywords or Reserved words are the words in
a language that are used for some internal
process or represent some predefined actions.
• These words are therefore not allowed
to use as a variable names or objects.
• Java also contains a list of reserved words
or keywords.
• Example: if, else, for, while, do, switch, int,
double, break, continue, new etc
IDENTIFIERS
• Identifiers in Java. Identifiers are the names of
variables, methods, classes, packages and
interfaces.
• A valid identifier in java – Must begin with a
letter (A to Z or a to z), currency character ($) or
an underscore (_). Can have any combination of
characters after the first character. Cannot be a
keyword.
LITERALS
•  Any constant value which can be assigned to
the variable is called as literal/constant.
• 12, -4, 100 
• 5.6F, -10.25F, 6.76
• ‘a’, ‘7’, ‘\0’,’\n’
• “sam”, “computer”
• true , false
PUNCTUATORS
Punctuators (also known as separators) are:
[ ] – Brackets
( ) – Parentheses
{ } – Braces
, – Comma
; – Semicolon
: – Colon
* – Asterisk
= – Equal sign
# – Pound sign
Most of these punctuators also function as operators.
OPERATORS
• Operator in Java is a symbol which is used to
perform operations.
UNARY OPERATORS
• The unary operators require only one operand/
• They perform various operations such as
-incrementing/decrementing a value by one
-negating an expression
-inverting the value of a boolean
• Unary minus(-): This operator can be used to
convert a negative value to a positive one.
Unary plus(+): int a=-5, d=6;
int b= -a; (b=5)
int c=-d; (c=-6)
• ‘NOT’ Operator(!): This is used to convert true
to false or vice versa. Basically it reverses the
logical state of an operand.
var = !true; // var =false
INCREMENT OPERATORS (++)
Increment(++): It is used to increment the value of an integer.
It can be used in two separate ways:
Post-increment operator: (first use then change)
When placed after the variable name, the value of the operand is incremented but the
previous value is retained temporarily until the execution of this statement and it gets
updated before the execution of the next statement.
num = 5 ; num++; (output will be 5 , afterwards num will become 6))
Pre-increment operator: (first change then use)
When placed before the variable name, the operand’s value is incremented instantly.
num = 5 ; ++num; (num=6)
int n1=5, n2=5;
System.out.println("Post increment = " + n1++);
  System.out.println("Pre increment = " + ++n2);
output-
Post increment = 5
Pre increment = 6
DECREMENT OPERATORS (--)
Decrement(--): It is used to decrease the value of an integer.
It can be used in two separate ways:
Post-decrement operator: (first use then change)
When placed after the variable name, the value of the operand is decreased but the
previous value is retained temporarily until the execution of this statement and it gets
updated before the execution of the next statement.
num = 5 ; num--; (num=4)
Pre-decrement operator: (first change then use)
When placed before the variable name, the operand’s value is decreased instantly.
num = 5 ; --num; (num=4)
int n1=5, n2=5;
System.out.println("Post decrement = " + n1--);
  System.out.println("Pre decrement = " + --n2);
output-
Post decrement = 5
Pre decrement = 4
BINARY OPERATORS
• Binary operators are those operators that work
with two operands.

• 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.

Instance variables can be accessed Static variables can be accessed by


directly by calling the variable name calling with the class
inside the class. However, within static name ClassName.Vari
methods (when instance variables are
given accessibility), they should be
called using the fully qualified
name. ObjectReference.VariableName.
ACCESS SPECIFIERS
COMMENTS
Comments in Java are the statements that are not executed by
the compiler and interpreter. It can be used to provide
information or explanation about the variable, method, class or
any statement. It can also be used to hide program code for a
specific time. 
Types of comments:
1. Single-Line Comments //this is a java program
2. Multi-Line Comments
/**
java program
*/

You might also like