Arvutiteaduse instituut
  1. Kursused
  2. 2018/19 kevad
  3. Objektorienteeritud programmeerimine (Narva Kolledž) (LTAT.NR.003)
EN
Logi sisse

Objektorienteeritud programmeerimine (Narva Kolledž) 2018/19 kevad

  • 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
  • Arvutiteaduse instituut
  • Loodus- ja täppisteaduste valdkond
  • Tartu Ülikool
Tehniliste probleemide või küsimuste korral kirjuta:

Kursuse sisu ja korralduslike küsimustega pöörduge kursuse korraldajate poole.
Õppematerjalide varalised autoriõigused kuuluvad Tartu Ülikoolile. Õppematerjalide kasutamine on lubatud autoriõiguse seaduses ettenähtud teose vaba kasutamise eesmärkidel ja tingimustel. Õppematerjalide kasutamisel on kasutaja kohustatud viitama õppematerjalide autorile.
Õppematerjalide kasutamine muudel eesmärkidel on lubatud ainult Tartu Ülikooli eelneval kirjalikul nõusolekul.
Courses’i keskkonna kasutustingimused