Class and Java Variables
Lesson 5
NEIL A. MUTIA, MIT
Java Class Naming Convention
There are a few rules and conventions related to the naming of Class.
The rules are:
Java class names are case sensitive. The class name demo is not the same
as Demo or DEMO. With that, be careful when saving or running your
program.
Java class names must start with a letter, or the $ or _ character.
After the first character in a Java class name, the name can also contain
numbers (in addition to letters, the $, and the _ character).
Class names cannot be equal to reserved key words in Java. For instance,
the words int or for are reserved words in Java. Therefore you cannot
name your class int or for.
Braces, Brackets, and Parentheses
Braces ("curly braces")
• Braces are used to group statements and declarations.
• The contents of a class or interface are enclosed in braces.
{} Method bodies and constructor bodies are enclosed in braces.
Braces are used to group the statements in an if statement, a loop, or
other control structures.
Brackets ("square brackets")
[] • Brackets are used to index into an array.
Parentheses
() • Parentheses are used for two purposes: (1) to control the order of operations in an
expression, and (2) to supply parameters to a constructor or method.
Note: Everything must be in pair in the right order
Variables
A Java variable is a piece of memory that can
contain a data value. A variable thus has a data
type.
Variables are typically used to store information which your
Java program needs to do its job. This can be any kind of
information ranging from texts, codes (e.g. country codes,
currency codes etc.) to numbers, temporary results of multi
step calculations etc.
Variables Analogy
Actual Situation
Variables
Data Type Size Description
int (Integer) 4 bytes Stores whole numbers from -2,147,483,648 to
2,147,483,647
double 8 bytes Stores fractional numbers. Sufficient for storing 15
decimal digits
boolean 1 bit Stores true or false values
char (Character) 2 bytes Stores a single character/letter or ASCII values
String 2 bytes Stores combination of characters such as word,
sentence, or paragraph
Variables
A java variable can be declared and instantiated in a Java Program
Declaration = assignment of value might happen in the next lines of the
program
int age;
double percent;
String name;
boolean result;
char letter;
Instantiation = assignment of value happens along with the declaration
int age = 15;
double percent = 99.9;
String name = “John”;
boolean result = true; boolean result = false;
char letter = ‘F’;
Sample Java Program
Using Integer or int
Sample Java Program
Using Double
Sample Java Program
Using String
Sample Java Program
Using Boolean
Sample Java Program
Using Character
Variables
A java variable might contain a value or an expression
Value
int age = 15;
double percent = 99.9;
String name = “John”;
boolean result = True;
char letter = ‘F’;
Expression
int sum = 15 + 9;
double percent = 18.2 + 6;
int sum = variable1 + variable2;
Sample Java Program
int sum = 15 + 9;
Sample Java Program
double percent = 18.2 + 6;
Sample Java Program
int sum = variable1 + variable2;
Java Variable Naming Convention
There are a few rules and conventions related to the naming of
variables.
The rules are:
Java variable names are case sensitive. The variable name money is not
the same as Money or MONEY.
Java variable names must start with a letter, or the $ or _ character.
After the first character in a Java variable name, the name can also
contain numbers (in addition to letters, the $, and the _ character).
Variable names cannot be equal to reserved key words in Java. For
instance, the words int or for are reserved words in Java. Therefore you
cannot name your variables int or for.
Laboratory Activity
Create a java program that uses the 4 data types of variables. Your program must
display your personal information using variables and concatenation. There must
be two variables under String data type that will hold the values for your First
Name and Last Name. A character variable that will hold your middle initial (it
should contain period at the end). An integer variable that will hold your age.
Another string variable for your cellphone number. A Double variable that will hold
your height in Centimeter. Use the concept to concatenation to put labels on the
information that will be displayed on the CMD. Name your class as “MyInformation”
Sample Output:
My First Name: Neil
My Last Name: Mutia
My Middle Initial: A.
My Age: 29 years old
My Cellphone Number: +(63) 9302219414
My Height in Centimeter: 167.64