Professional Documents
Culture Documents
Chapter03 Variable & Datatypes
Chapter03 Variable & Datatypes
com
+91 9582815419
Chapter 03
Variable & Datatypes
YuktiSoftwares
1 www.yuktisoftwares.com
+91 9582815419
www.yuktisoftwares.com
+91 9582815419
Contents
Variable in Java ....................................................................................................................................... 3
Variables in Java: ................................................................................................................................. 3
Variables in Java are case-sensitive. ................................................................................................... 3
Data type ................................................................................................................................................. 7
Primitive data types: ........................................................................................................................... 7
Non-primitive data types: ................................................................................................................... 7
Java Keywords : ....................................................................................................................................... 4
Java Literals: ............................................................................................................................................ 8
Boolean Literals................................................................................................................................... 8
There are two boolean literals ................................................................................................................ 8
true represents a true boolean value ..................................................................................................... 8
false represents a false boolean value.................................................................................................... 8
There are no other boolean literals, because there are no other boolean values!................................ 8
Character Literals ................................................................................................................................ 8
Primitive Data Types ........................................................................................................................... 8
2 www.yuktisoftwares.com
+91 9582815419
www.yuktisoftwares.com
+91 9582815419
Variable in Java
Variables in Java:
A variable is a location in memory to hold data.
Java convention to declare variables: int maxSize = 10;
Here maxSize is variable name whose data type is int and 10 is the value assign
to the variable.
Rule for naming variables in Java:
When creating variables, choose a name that makes sense. For example, score,
number, level makes more sense than variable names such as s, n, and l.
If you choose one-word variable names, use all lowercase letters. For example,
it's better to use speed rather than SPEED, or sPEED.
If you choose variable names having more than one word, use all lowercase
letters for the first word and capitalize the first letter of each subsequent
word. For example, speedLimit.
3 www.yuktisoftwares.com
+91 9582815419
www.yuktisoftwares.com
+91 9582815419
Java Keywords :
4 www.yuktisoftwares.com
+91 9582815419
www.yuktisoftwares.com
+91 9582815419
Best Practices:
1. When creating variables, choose a name that makes sense. For
example, score, number, level makes more sense than variable names
such as s, n, and l.
2. One-character variable names should be avoided except for temporary
variables.
3. If you choose one-word variable names, use all lowercase letters. For
example, it's better to use speed rather than SPEED, or sPEED.
4. If you choose variable names having more than one word, use all
lowercase letters for the first word and capitalize the first letter of each
subsequent word. For example, speedLimit.
Camel case in Java Programming: It consists of compound words or phrases such that
each word or abbreviation begins with a capital letter or first word with a
lowercase letter, rest all with capital.
Examples:
interface Bicycle
class MountainBike implements Bicyle
interface Sport
class Football implements Sport
2. Methods:
• Methods should be verbs, in mixed case with the first letter
lowercase and with the first letter of each internal word capitalized.
Examples:
void changeGear(int newValue);
void speedUp(int increment);
5 www.yuktisoftwares.com
+91 9582815419
www.yuktisoftwares.com
+91 9582815419
3. Constant variables:
• Should be all uppercase with words separated by underscores (“_”).
• There are various constants used in predefined classes like Float,
Long, String etc.
Examples:
static final int MIN_WIDTH = 4;
// Some Constant variables used in predefined Float class
public static final float POSITIVE_INFINITY = 1.0f / 0.0f;
public static final float NEGATIVE_INFINITY = -1.0f / 0.0f;
4. Packages:
• The prefix of a unique package name is always written in all-
lowercase ASCII letters and should be one of the top-level domain
names, like com, edu, gov, mil, net, org.
• Subsequent components of the package name vary according to an
organization’s own internal naming conventions.
Examples:
com.sun.eng
com.apple.quicktime.v2
6 www.yuktisoftwares.com
+91 9582815419
www.yuktisoftwares.com
+91 9582815419
Data type
Data types specify the different sizes and values that can be stored in the
variable. There are two types of data types in Java:
Primitive data types: The primitive data types include boolean, char, byte,
short, int, long, float and double.
Note : Java is a statically-typed language. This means that all variables must be declared
before they can be used.
7 www.yuktisoftwares.com
+91 9582815419
www.yuktisoftwares.com
+91 9582815419
Java Literals:
Java Literals are syntactic representations of boolean, character, numeric, or string data. Literals
provide a means of expressing specific values in your program. For example, in the following
statement, an integer variable named count is declared and assigned an integer value. The literal 0
represents, naturally enough, the value zero.
Boolean Literals
There are two boolean literals
true represents a true boolean value
false represents a false boolean value
There are no other boolean literals, because there are no other boolean values!
Character Literals
Character literals are constant valued character expressions embedded in a Java program. Java
characters are sixteen bit Unicode characters, ranging from 0 to 65535. Character literals are
expressed in Java as a single quote, the character, and a closing single quote ('a', '7', '$', 'π').
Character literals have the type char, an unsigned integer primitive type. Character literals may be
safely promoted to larger integer types such as int and long. Character literals used where a short or
byte is called for must be cast to short or byte since truncation may occur.
A primitive data type specifies the size and type of variable values, and it has no additional methods.
There are eight primitive data types in Java:
8 www.yuktisoftwares.com
+91 9582815419