Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

www.yuktisoftwares.

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:

Variables in Java are case-sensitive.


A variable's name is a sequence of Unicode letters and digits. It can begin with
a letter, $ or _. However, it's a convention to begin a variable name with a
letter. Also, variable names cannot use whitespace 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.

Rule for naming variables in Java :

1. Variables in Java are case-sensitive.


Ex:-Abc, abc ABC are different

2. A variable's name is a sequence of Unicode letters and digits. It can


begin with a letter, $ or _.

3. However, it's a convention to begin a variable name with a letter. Also,


variable names cannot use whitespace in Java.

3 www.yuktisoftwares.com
+91 9582815419
www.yuktisoftwares.com
+91 9582815419

Java Keywords :

abstract continue for new switch


assert default goto package synchronized
boolean do if private this
break double implements protected throw
byte else import public throws
case enum instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp volatile
const float native super while

Java literals: A Literal is the source code representation of a fixed value.

boolean flag = false;

Here, Boolean is data type, flag is variable and False is literal.

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.

1. Classes and Interfaces:


• Class names should be nouns, in mixed case with the first letter of
each internal word capitalized. Interfaces name should also be
capitalized just like class names.
• Use whole words and must avoid acronyms and abbreviations.

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

void applyBrakes(int decrement);

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

// java.lang packet in JDK


java.lang

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.

Non-primitive data types: The non-primitive data types


include Classes, Interfaces, and Arrays.

Default Value Default Size


Data Type
boolean false 1 bit
char '\u0000' 2 byte(2X8bits)
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
float 0.0f 4 byte
double 0.0d 8 byte

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.

Primitive Data Types

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:

Data Type Size Description


byte 1 byte Stores whole numbers from -128 to 127
short 2 bytes Stores whole numbers from -32,768 to 32,767
int 4 bytes Stores whole numbers from -2,147,483,648 to
2,147,483,647
long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808
to 9,223,372,036,854,775,807
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7
decimal digits
double 8 bytes Stores fractional numbers. Sufficient for storing 15
decimal digits
boolean 1 bit Stores true or false values
char 2 bytes Stores a single character/letter or ASCII values

8 www.yuktisoftwares.com
+91 9582815419

You might also like