Skip to main content

Variables, Data Types and Literals in Java

Hi! Lets continue with our next topic!

VARIABLES

What is a Variable?

A variable is just like a bottle. A bottle can contain only liquid and it can be of any type i,e, water, juice, milk, etc. For example, a bottle is of size 1 ltr. and filled with water. Note that the type and quantity of liquid is now determined i.e. the bottle can contain only 1 ltr. of water and not more than that.Similarly, a variable is a fixed size container depending upon its data type.. It also compulsorily has a name.

There are some naming conventions which will be discussed later on! (camel case).
Let's see the types of variables!
There are three types of variables :

  1. Local / Global variables
  2. Instance / reference variables
  3. Static variables

DATA TYPES

What are Data Types in Java?

Java is a strongly typed language i.e. a variable needs a data type before it holds some data. If you want to fill a container and you dont know what is going to filled in that container, java compiler or javac gives an error.
data-types-in-java-table
Table 6.1 Primitive data types with sizes and examples.

Note : Java thinks anything with a floating point is a double, unless you use ‘f’('f' can be uppercase or lowercase) Similarly, for long use 'L'(lowercase/uppercase).


Following chart shows all the valid data types in Java.
Data-types-in-java
Data Types in Java
(Click on image to view)






  .

LITERALS

What are Literals in Java?

Java Literals are a sequence of digits/alphabets/other characters that represent constant values to be stored in variables. Java specifies 5 types of literals namely integer, floating, string, character, boolean literals.

Rules for Number Literals...

  • underscore can be placed only between digits
  • underscore is not allowed  at following places
  • at the beginning /end of a number
  • besides a decimal point of floating point literals
  • before f/L suffix

Rules for Character Literals...

  • any printable character except backslash can be specified as character literal
some characters cannot be directly printed, so they are preceded with backslash; known as Escape Sequences. Refer following table...
Escape-character-sequences
Tab. 6.2 Escape Character Sequences

Camel Case  in Java

Generally in English, upper/lower case is used for obvious reasons.
Camel case is a writing convention in Java that makes it easier for any Java programmer to identify the type of blocks. Following are the types of camel cases in Java :
  1. Upper camel case
  2. Lower camel case

Lower camel case

Lower camel case can be clearly understood by this e.g. 
myFirstExampleOfCamelCase
It is clearly seen from the e.g. that since first letter of the word is lower case but is combined with many meaningful words with first letter of each letter as capital.
This is what camel case is!
The compound word seems similar to the humps of camel having more than one humps.
LowerCamelCase is used for naming variables, references, objects, method names.

Upper camel case

Example for Upper camel case 
MySecondExampleOfCamelCase.
The only difference between both the cases is that upper camel case has first letter of the compound word as capital.
UpperCamelCase is used for naming classes and interfaces.

Comments