Language Elements
Language Elements
Overview
●
Statements and expressions
●
Variables and primitive data types
●
Constants
●
Comments
●
Literals
●
Arithmetic
●
Comparisons
●
Logical operators
Statements and expressions
●
A statement is a command that causes something to happen.
Example:
int weight = 225;
System.out.println(“My weight is ” + weight);
●
An expression is a statement that produces a value. The value produced by the statement is
called a return value.
Example:
int sum = add(x, y);
Variables
●
Used to store information (data) while the program is running.
●
Types of variables:
– Class variables - used to defne the attributes for an entire class of objects and apply to all
instances of it.
– Instance variables - used to defned the object's attributes.
– Local variables - used inside method defnitions or even smaller blocks of statements within a
method.
Declaring variables
●
Variable declaration:
– int loanLength;
– String message;
– boolean gameOver;
●
Assigning initial values:
– int zipCode = 1000;
– String city = “Skopje”;
Naming variables
●
The name must start with a letter, an underscore character ("_"), or a dollar sign ("$"). In practice, prefer
to use letters only.
●
Names are case sensitive.
●
CamelCase notation:
– The frst letter of the variable name is lowercase.
– Each successive world in the variable name begins with a capital letter.
– All other letters are lowercase.
– Examples:
●
Button loadFile;
●
int localAreaCode;
●
boolean quitGame;
Variable types
A variable in Java can be declared as one of these three types:
– One of the primitive data types
– The name of a class or interface
– An array
Primitive data types
Eight primitive data types are part of the Java language:
– Four to store integers:
●
byte (8 bits) - 128 to 127
●
short (16 bits) - 32,768 to 32,767
●
int (32 bits) - 2,147,483,648 to 2,147,483,647
●
long (64 bits) - 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
– Two foating-point numbers:
●
foat - 1.4E-45 to 3.4E+38
●
double - 4.9E-324 to 1.7E+308
– The char type is used for individual characters such as letters, numbers, punctuation, and other symbols.
– The boolean type can hold either a true or a false value.
Class types
A variable can be of Java's built-in classes, a third-party class or a class explicitly defned in
the project.
Examples:
– String lastName = “Hopper”;
– Color hair;
– VolcanoRobot vr;
Assigning values to variables
Assigning a value is done using the assignment operator which is the equal sign ("=")
Examples:
– idCode = 8675309;
– accountOverdrawn = false;
Constants
●
A constant is a variable which is not allowed to change its value as the program runs.
●
In Java, constants are defned using the fnal keyword.
●
Examples:
– final float PI = 3.141592;
– final boolean DEBUG = false;
– final int PENALTY = 25;
Comments
Used to improve the readability of the program.
– Single line comments - denoted by two slash characters "//“.
Example:
int creditHours = 3; // set up credit hours for course
Expression Meaning
x += y x=x+y
x -= y x=x-y
x *= y x=x*y
x /= y x=x/y
Increment and decrement operators
●
Incrementing and decrementing are used very often so Java provides special operators to make this less verbose.
Examples:
– int x = 7;
– x++; // The new value is 8.
●
When using in an expression, it's important whether the increment or decrement operator is used in a prefx (e.g. +
+x) or a postfx (e.g. x++) notation.
Examples:
– int x, y, z; // x, y, and z are all declared.
– x = 42; // x is given the value of 42.
– y = x++; // y is given x’s value (42) before it is incremented and x is then incremented
to 43.
– z = ++x; // x is incremented to 44, and z is given x’s value.
Comparison operators