Java Fundamentals
Information Technology Computer Science 2024
Page 02
Java Identifiers
All Java components require names. Names used for
classes, variables, and methods are called identifiers.
Information Technology | Computer Science | 2024
Page 03
Overview
• All identifiers should begin with a letter (A to Z or a to z),
currency character ($) or an underscore (_).
• After the first character, identifiers can have any combination
of characters.
• A key word cannot be used as an identifier.
• Most importantly, identifiers are case sensitive.
Information Technology
Page 04
Example of a legal identifier
Computer Science
age, $salary, _value, __1_value.
Example of a illegal identifier
2024
123abc, -salary.
Page 05
Java Modifiers
Access Modifiers
default, public , protected, private (controls the access
level)
Non-access Modifiers
final, abstract, strictfp (do not control
access level but provides other
functionality)
Information Technology | Computer Science | 2024
Page 06
Java Variables
Following are the types of variables in
Java:
·Local Variables
·Class Variables (Static Variables)
·Instance Variables (Non-static Variables)
Information Technology | Computer Science | 2024
Page 07
Java Variables
Example:
int a, b, c; // Declares three integers, a, b, and c.
int a = 10, b = 10; // Example of initialization
byte B = 22; // initializes a byte type variable B.
double pi = 3.14159; // declares and assigns a value of PI.
char a = 'a'; // the char variable a is initialized with value
'a'
Information Technology | Computer Science | 2024
Page 08 Kinds of Variables
Local Variable
·Local variables are declared in methods, constructors, or blocks.
·Local variables are created when the method, constructor or block is
entered and the variable will be destroyed once it exits the method,
constructor, or block.
·Access modifiers cannot be used for local variables.
·Local variables are visible only within the declared method,
constructor, or block.
·Local variables are implemented at stack level internally.
·There is no default value for local variables, so local variables should
be declared and an initial value should be assigned before the first use.
Page 09 public class Test {
public void pupAge() {
int age = 0;
age = age + 7;
[Link]("Puppy age is : " + age);
}
public static void main(String args[]) {
Test test = new Test();
Example
[Link]();
}
}
Output
Puppy age is: 7
Information Technology | Computer Science | 2024
Page 10
Instance Variables
Information Technology | Computer Science | 2024
Page 11
Example
Information Technology Computer Science 2024
Page 13
Class/Static
Variables
Information Technology | Computer Science | 2024
• Byte Page 13
• Short
Primitive Data types
• Int
• Long
• Float
• Double
• Boolean
• Char
Information Technology | Computer Science | 2024
Page 14
Example:
Reference Data Type
·Reference variables are created using defined constructors of the
classes. They are used to access objects. These variables are
declared to be of a specific type that cannot be changed. For
example, Employee, Puppy, etc. Animal animal = new Animal("giraffe");
·Class objects and various type of array variables come under
reference datatype.
·Default value of any reference variable is null.
·A reference variable can be used to refer any object of the
declared type or any compatible type.
Information Technology| Computer Science | 2024
Page 15 Information Technology| Computer Science | 2024
Java Literals
·A literal is a source code representation of a fixed
value. They are represented directly in the code
without any computation.
byte a = 68;
char a = 'A';
Page 16
Basic Operators
Information Technology | Computer Science | 2024
Page 17
Relational Operators
Information Technology Computer Science 2024
Page 18
Bitwise Operator
Information Technology | Computer Science | 2024
Page 19
Assignment Operators
Information Technology Computer Science 2024