Topic 3_Elements of Java Programming (1)
Topic 3_Elements of Java Programming (1)
1. Alphabets
1. CHARACTER SET
A character set is a
Java accepts both lowercase and uppercase alphabets as variables and methods.
set of alphabets,
letters and some
2. Digits
special characters
that are valid in Java
language.
3. Special Characters
Keywords are predefined, reserved words used in programming that have special
meanings to the compiler.
Java Keywords
abstract boolean break private do synchronized try
case catch char short final transient package
The area or the region of a program where a variable can be accessed is known as
variable scope. The various types of variables on the basis of the variable scope.
1. Class Variable: Are declared inside a class before their use. Class variables are
accessible within a class and its objects.
2. Instance Variable: Are declared inside a class and are created when the class is
instantiated. Objects are given different values to instance variables as per
requirement.
3. Local Variable: Are declared inside a method. Their scope is within the block of code
in which they are defined, and are not accessible outside the method.
4. Static Variables: Are allocated memory only once but are globally accessible to all
instance of a class. Therefore, when an instance of a class is destroyed, the static
variable is not destroyed and is available to other instance of that class.
4. JAVA LITERALS
Literals are the values to be stored in variables and constants. A literal contains a
sequence of characters, such as digits, alphabets, or any other symbol that
represents the value to be stored.
Types of Literals
1. Integers Literals :
Are numeric type values with whole parts.
2. Floating-point Literals
Are numeric values with fractional parts.
3. Character
Are enclosed in single quotation marks.
4. String Literals
Are enclosed in double quotation marks.
5. Boolean Literals
Are the literals having value, true or false.
JAVA LITERALS
For example:
An integer is a numeric literal(associated with
• decimal (base 10): 0, -9, 22 etc
numbers) without any fractional or exponential • octal (base 8): 021, 077, 033 etc
part. There are three types of integer literals in • Hexadecimal (base 16): 0x7f,
Java programming: 0x2a, 0x521 etc
If you want to define a variable whose value cannot be changed, you can use the
const keyword. This will create a constant.
For example,
The data stored in memory of the computer can be of many types. For example, a
person’s age is stored as a numeric value and an address is stored as alphanumeric
characters. Data types are used to define the operations possible on variables and the
storage method.
Primitive Data Types: These are the built-in data types. There are eight primitive
data types in Java, which are further grouped in the following categories.
▪ Integer type: Byte, short, int and long
▪ Floating point type: float and double
▪ Boolean type
▪ Character type
Default
Group Data Type Size (bytes) Range Value
Abstract Data Types: These are derived from the primitive data types and have
more functions than primitive data types. For example, String is an abstract data type
that can store letters, digits and other characters, such as /, (), :, ;, $, and #.
7. Escape Sequence Escape Character
Sequence
Escape Sequence are characters that cannot \b Backspace
be typed.
\f Form feed
\n Newline
\r Return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\’ Single quotation
mark
\” Double quotation
mark
\? Question mark
\0 Null character
8. JAVA OUTPUT
Java programming language provides a set of built-in methods to output the data on the
computer screen.
System.out.println(); or
System.out.print(); or
System.out.printf();
Here,
▪ System is a class
▪ Out is a public static field: It accepts output data.
import java.util.Scanner;
Then, we need to create an object of the Scanner class. We can use the object to take
input from the user.
We will create an object named input of the Scanner class. We then call the nextInt()
method of the Scanner class to get an integer input from the user.
Similarly, we can use nextLong(), nextFloat(), nextDouble(), and next() methods to get
long, float, double, and string input respectively from the user.
10. JAVA COMMENTS
In programming, comments are hints that a programmer can add to make their
code easier to read and understand.
For example
Use of Comments in Java
// declare and initialize two variables 1. Make Code Easier to Understand
int a =1; If we write comments on our code, it will be easier to
int b = 3; understand the code in the future.