0% found this document useful (0 votes)
4 views

04 Java Fundamental

Uploaded by

amitchaurasia774
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

04 Java Fundamental

Uploaded by

amitchaurasia774
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

04 Java Fundamental

PRAVEEN KUMAR SRIVASTAVA


DECLARING VARIABLES AND
LITERALS

Java supports some basic programming elements, such as data types, keywords,
literals, and variables. Keywords are the reserved words for Java programming
language, which cannot be used as names for variables, class, or method.

Java is a strictly typed language, which means that Java gives importance to type
checking. Expressions and variables in Java can be of different types, such as int,
char, or string. Type checking is one of the important steps in compilation of a
program. The data stored in the memory of a computer can be of different types.
The various data types in Java are:

 Primitive or the simple data types


 Abstract or the derived data types
Primitive Data Types

The built-in data types in Java are known as the primitive or the simple data types.
There are eight primitive data types in Java, which are further grouped in the following
categories:

Integer type: Can store whole number values. The size of the values of the
variables depends upon the chosen integer data type. The four integer data
types are:
 byte
 short
 int
 long
Floating point type: Can store fractional numbers. The two types of floating
point type are:
 float
 Double

Boolean type: Can store only the true and false values. Boolean data type is
required when a condition has to be checked. The true or false value of the
expression or the condition determines further execution of the Java program.

Character type: Can store symbols, such as letters and numbers. In character
type, there is one data type, char.
The following table lists the primitive data types with their size and
range, grouped in four categories:
Abstract data types

The abstract data types include the data types 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 #. You cannot perform calculations on a
variable of the String data type even if the data stored in it has digits. However, String
provides methods for concatenating two strings, searching for one string within another,
and extracting a part of a string.
Keywords

The keywords are the reserved words for a language, which express the language
features. Keywords cannot be used for naming purpose of variables, constants, or
classes.
Defining Variables and Literals

A variable is used to store and manipulate data or values in programs. A variable is


the name that refers to a memory location where some data value is stored.

Variable is defined as “variable is a named memory space area where we can store
user input values”.

Each variable that is used in a program must be declared. For example, if the
variable, number, is to be used for storing an integer value, the variable, number,
must be declared and it should be of the type int.
Naming Conventions for Variables

A program refers to a variable by using its name. Certain rules and conventions
govern the naming of variables. These rules are enforced by a programming
language. A program does not compile if you have not followed the rules of the
language.
Conventions help to improve the readability of the program. The naming conventions
for a variable in Java are:
 The name of a variable needs to be meaningful, short, and without any
embedded space or symbol, such as ?, !, #, @, %, &, {}, [], :, ;, “, and /.

 A variable name must be unique.


 A variable name must begin with a letter, an underscore (_), or the dollar
symbol ($), which can be followed by a sequence of letters or digits (0 to 9), ‘$’,
or ‘_’.
 A variable name should not start with a digit.
 A variable name should not contain embedded white spaces. You can use an
underscore for spacing purpose.
 A variable name should not consist of a keyword.
 A variable name in Java is case sensitive. There is a difference between
uppercase and lowercase names.
For example, “age” is not same as “Age”.
Types Of Variable

The various types of variables on the basis of the variable scope in Java are:

 Class variables: Are accessible within a class and its objects. The class variables
are declared inside the class before their use.

 Instance variables: Are declared inside a class and are created when the class is
instantiated. Objects give different values to instance variables as per the
specific requirements of the object of that class type.
 Local variables: Are declared inside a method. Their scope is within the block of
code in which they are defined. They are local to the block of code and are not
accessible outside the method.

 Static variables: Are allocated memory only once but are globally accessible to
all instances of a class. Therefore, when an instance of a class is destroyed, the
static variable is not destroyed and is available to other instances of that class.

 Automatic variables: Are accessible within the function in which they are
declared. The automatic variables are created when a function is called and are
destroyed when you exit the function.
1001
Roll No

“Amit Kumar”
Name
Student
“15 /07/1999”
DOB

“Sigra, Varanasi”
Address

[email protected]
Email
Declaring Variable

The declaration of a variable informs the compiler about the variable name, data type,
and the scope of a variable. A variable has to be declared before you access it. The
following syntax shows how to declare a variable:
First Way
<DataType> <VariableName>; int RollNo;

Second Way
<DataType> <VariableName>=<value>; int RollNo=1001;
Literals in Java

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. The various types of literals in Java are:

 Integer literals: Are numeric type values. The numerical values can be
represented in octal and hexadecimal notation. The octal notation of a number is
prefixed by zero and hexadecimal numbers are prefixed by 0x. For example,
n=0123 is integer literal in octal notation, n=0x456 is integer literal in
hexadecimal notation, and n=2 is decimal notation for an integer literal.

 Floating point literals: Are numeric values with fractional part. For example,
x=7.9 is a floating point literal.
 Character literals: Are represented in single quotation marks. For example, x='k'
is a character literal.

 String literals: Are enclosed in double quotation marks. For example, x="James"
is a string literal.

 Boolean literals: Are the literals having value, true or false. For example,
x= false is a Boolean literal.

You might also like