Institute of Computer Science
  1. Courses
  2. 2018/19 spring
  3. Object-Oriented Programming (Narva College) (LTAT.NR.003)
ET
Log in

Object-Oriented Programming (Narva College) 2018/19 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:

  • use camel case notation;
  • do not contain special characters or spaces;
  • do 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 not only the name of a variable (as in Python), but also the type of data to be stored there. This is called variable declaration.

Here is an example of a variable declaration which will hold a text (string):

 String var;

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

 var = "Hi!";

The previous two lines can be merged:

 String var = "Hi!";

In Python, it is possible to declare several variables on the same line using a semicolon, e.g. a = "Hi!"; b = "World!". In Java, we can also declare several variables on the same line but only if they are of the same data type:

  String var1 = "Hi!", var2 = "World";

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

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