string(175) "LC_CTYPE=C.UTF-8;LC_NUMERIC=en_US.utf8;LC_TIME=C;LC_COLLATE=C;LC_MONETARY=C;LC_MESSAGES=C;LC_PAPER=C;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=C;LC_IDENTIFICATION=C" string(6) "PART1E" Objektorienteeritud programmeerimine (Narva Kolledž) - Kursused - Arvutiteaduse instituut
Arvutiteaduse instituut
  1. Kursused
  2. 2017/18 kevad
  3. Objektorienteeritud programmeerimine (Narva Kolledž) (P2NC.01.083)
EN
Logi sisse

Objektorienteeritud programmeerimine (Narva Kolledž) 2017/18 kevad

  • Home
  • Materials
  • Grading
  • Source Example
  • Links
Session 1

Data types

As many other programming languages, Java uses variables to represent values.

PS! Pay attention, the variable names:

  • 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 numbers;
  • cannot be composed solely of numbers.

Variables must be declared before they are used.

Unlike Python, you must tell Java what type of data the variable will store. For example, if we need to use a variable which contains an integer, we have to define the variable data type and the variable name as follows:

 int var;

This declares a variable and its data type as well as reserves memory for it. 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.

In general, there are eight primitive data types in Java:

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! Strings are not primitive data type in Java. It is an instance of class String.

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