Institute of Computer Science
  1. Courses
  2. 2017/18 spring
  3. Object-Oriented Programming (Narva College) (P2NC.01.083)
ET
Log in

Object-Oriented Programming (Narva College) 2017/18 spring

  • Home
  • Materials
  • Java Glossary
  • Source Example
  • Cheat sheet (S1-S6)
  • Grading
  • Links
Session 1

Variables

As many other programming languages, Java uses variables (a named memory location) to store values.

PS! Pay attention, the variable names must meet the following rules:

  • the first character is always written in lower case when using the camel case notation;
  • must not contain special characters or spaces;
  • must not start with a digit.

Variables must be declared before they are used.

To use a variable in Java, we have to declare it by telling the compiler the name of the variable as well as the type of data to be stored there. This is called variable declaration.

Here is an example of integer type variable declaration:

 int var;

However, the statement says nothing about the value. To do so, an assignment statement = must be used:

 var = 5;

The previous two lines can be merged:

 int var = 5;

We can also declare two variables of the same data type, reserve memory, and put an initial value in each variable as well:

  int var1 = 5, var2 = 15;

PS! We can declare a variable once. If we try to do it twice, Java will through out an error.

Primirive data types

Java is a strongly typed language. This means that all operations are type-checked by the compiler for type compatibility. Illegal operations will not be compiled. Thus, strong type checking helps prevent errors and enhances reliability. To enable strong type checking, all variables, expressions, and values have a type. There is no concept of a "type-less" variable in Java. Furthermore, the type of a value determines what operations are allowed on it. An operation allowed on one type might not be allowed on another.

Java contains two general categories of built-in data types: object-oriented and non-object-oriented. Java object-oriented types are defined by classes, and a discussion of classes will be later in several sessions.

However, at the core of Java are eight primitive (also called elemental or simple) types of data. The term primitive is used here to indicate that these types are rather normal binary values. All of Java other data types are constructed from these primitive types.

Java built-in primitive data types:

Data typeRange
byte-128 to 127
short-32 768 to 32 767
int-231 to 231
long-263 to 263
float-3.4x1038 to 34x1038
double-1.8x10308 to 1.8x10308
booleantrue or false
charany symbol represented in 16-bit Unicode from '\u0000' to '\uFFFF'.

PS! Java strictly specifies a range and behavior for each primitive type. PSS! Strings are not primitive data type in Java. It is an instance of class String.

Literals

In Java, literals refer to fixed values that are represented in their human-readable form. Java literals can be of any of the primitive data types. The way each literal is represented depends upon its type (e.g. the integer 123 , the decimal 45.67, the character 'a' and the string "Hello, World!" are literals).

Beginning with JDK 7, we can embed underscores _ into an integer or floating-point literals, e.g. 123_456_789 is the same as 123456789. Doing so can make it easier to read values consisting of many digits. When the literal is compiled, the underscores are ignored.

Session 1
  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment