What are attributes
Lesson Content
- Attributes - definition, and meaning
- Simple and complex data types in Java
- Java naming convention for attributes
- Default and initial values
- Code formatting: indentation
- Most common errors
Attributes - definition, and meaning
As already mentioned, attributes represent
certain characteristics (properties) of classes.
For the class Person, these might be: name,
surname, gender, age, etc. These characteristics are
often expressed through some number, letter, or string
of characters.
Defining attributes is relatively simple and looks like this:
1 data_type attributeName;
Simple and complex data types in Java
A data type represents a set of possible values for an attribute – integers, real numbers, letters, strings of characters, or something else.
Some of the most commonly used data types in Java are:
- int – integers, e.g., 1, -55, 0, 1000000.
- double – real numbers, e.g., 12.55, -234.77, 0.21, 6.371e6 (which is the same as 6371000.0), 6.371e-2 (which is the same as 0.06371).
- char – a single character (letter, digit, or some other character), e.g., ‘a’, ‘A’, ‘e’, ‘!’, ‘;’, ’ ’ (space), ‘4’, ‘9’.
- boolean – a logical variable, can only have the values true or false (one of these two values).
- String – a string of characters, e.g., “River”, “Pera”, “123”, “zx!”.
- LocalDateTime – date and time, e.g., 2008-12-31 10:50.
It should be noted that the double type can also receive an integer value (for example, -34), but Java will automatically convert this to a real number (i.e., decimal format -34.0) before assigning it. The reverse does not apply, i.e., if we try to assign 2.55 to an int variable, it will result in a syntax error.
Java naming convention for attributes
According to the Java naming convention, attribute names should start with a lowercase letter:
- age
- brand
- model, etc.
If the attribute name consists of multiple words, all words are written together. The first word starts with a lowercase letter, and all other words start with uppercase letters:
- socialSecurityNumber
- firstName
- engineNumber, etc.
An important rule in Java is that all commands must end with a semicolon (;). So, the attribute declaration ends with a semicolon.
Default and initial values
Types like int, double, char, and boolean are simple data types, while String and LocalDateTime are complex types. String is a sequence of characters, while LocalDateTime contains day, month, year, hour, minute, etc. All complex data types in Java are represented by using classes, so String and LocalDateTime are two predefined Java classes.
It is important to note that there are other simple data types, but they are not used as frequently: long (a set of integers with a range greater than the int type), short (a set of integers with a smaller range than int), and float (a set of real numbers, but with fewer decimal places than the double type).
Each variable in Java gets a default value when declared unless a different initial value is explicitly specified in the code. The default value depends on the type, so for example:
- int and other integer variables receive the default value of zero (0).
- double and other real number variables receive the default value of zero in decimal format (0.0).
- boolean variables receive false.
- All complex-type variables (any class) receive the default value of null.
Also, in Java, char values (characters) must be written in single quotation marks (e.g., ‘A’), while String values must always be written in double quotation marks. For example, “BANANA” is a String value, but “A” is also a String value (even though it contains only one letter) because it’s enclosed in double quotation marks (a char value would be ‘A’).
Example 1
Create a class called CashMachine. This class should only have the attribute “balance” representing the amount of money currently in the machine (real number).
1 class CashMachine {
2 double balance;
3 }
Example 2
Create a class called Computer. This class should have the following attributes: processor speed (real number, e.g., 4.0 GHz), ram (real number, e.g., 2.0 GB), and hard disk (integer, e.g., 120 GB).
1 class Computer {
2 double processorSpeed;
3 double ram;
4 int hardDisk;
5 }
The order in which attributes are defined within the class is not important. What matters is that all necessary attributes are declared with the appropriate types and names.
Example 3
Write the code for the Computer class from the previous example in multiple ways, changing the order of the attribute declarations (multiple solutions are given).
1 class Computer {
2 double ram;
3 double processorSpeed;
4 int hardDisk;
5 }
or
1 class Computer {
2 int hardDisk;
3 double ram;
4 double processorSpeed;
5 }
or
1 class Computer {
2 double processorSpeed;
3 int hardDisk;
4 double ram;
5 }
It’s possible to assign an initial value to an attribute when defining the class. This is not the default value assigned by Java, but the programmer can provide an initial value for the attribute if necessary. The default value is assigned where the programmer hasn’t specified an initial value. The initial value is assigned to an attribute in the following way:
1 data_type attributeName = value;
Example 4
Modify the Computer class from previous examples so that the initial value for the processor speed is 4.0 (GHz), ram is 16.0 (GB), and the hard disk has 500 (GB). Save the class as a new class, Computer2.
1 class Computer2 {
2 double processorSpeed = 4.0;
3 double ram = 16.0;
4 int hardDisk = 500;
5 }
To shorten the declaration of attributes, it’s possible to combine declarations of attributes of the same type into one declaration as follows:
1 data_type attributeName1, attributeName2, attributeName3;
Or, if initial values are required for these attributes, they can be assigned like this:
1 data_type attributeName1 = value1, attributeName2 = value2, attributeName3 = value3;
Example 5
Create a class called Person. This class should have the following attributes:
- name (String, initial value “unknown”)
- surname (String, initial value “unknown”)
- height (real number, e.g., 186.5 cm)
- weight (real number, e.g., 80.5 kg)
- gender (char)
Write the code for this class in two ways:
- Without combining declarations of attributes of the same type – class Person.
- With combining declarations of attributes – class Person2.
1 class Person {
2 String name = "unknown";
3 String surname = "unknown";
4 double height;
5 double weight;
6 char gender;
7 }
8
9 class Person2 {
10 String name = "unknown", surname = "unknown";
11 double height, weight;
12 char gender;
13 }
Code Formatting: Indentation
It has already been mentioned that breaking commands into multiple lines and adding spaces is done to make the code more readable for the programmer – in terms of compiling, it doesn’t matter. Here, we should also mention that a typical transformation is used to increase readability: indentation of code (shifting lines of code to the right).
Code indentation, along with breaking commands into multiple lines, is part of the code formatting rules. The idea of indentation is that all code within a block of commands (e.g., the body of a class) is “shifted to the right” relative to commands outside that block (e.g., relative to the class declaration). This means that the code is not written from the left edge but is moved a few spaces to the right (usually four to eight spaces, or one tab character). The closing curly brace that marks the end of the block remains aligned with the left edge. Also, any code within the body of a method (a new block of commands) should be indented an additional 4-8 spaces compared to the method declaration, and so on. Usually, the programming environment, or more precisely its code editor, automatically handles indentation.
Example 6
Let’s take the Person class from the previous example with the attributes name, surname, height, weight, and gender. Without following the code formatting rules, this class could be written like this:
1 class Person {String name = "unknown";String surname = "unknown";double height;double weight;char gender;}
This code is less readable because everything is written in one line. If the rules for breaking commands into new lines are followed, the code becomes more readable:
1 class Person {
2 String name = "unknown";
3 String surname = "unknown";
4 double height;
5 double weight;
6 char gender;
7 }
This code is more readable, but each command is written at the left edge without indentation. This makes it hard to see that the attributes name, surname, height, weight, and gender are part of the block of commands that form the class body. If this class had methods (and each method has its own declaration and body forming a new block of commands), this would be even harder to notice.
If we now introduce the rules for code indentation (each command within the body of the class is indented six spaces or one tab relative to the class declaration), the code becomes even more readable – which is the ultimate goal:
1 class Person {
2 String name = "unknown";
3 String surname = "unknown";
4 double height;
5 double weight;
6 char gender;
7 }
Most common errors
Some of the most common syntax errors related to attribute declarations and assigning initial values are:
Incorrectly writing reserved words int, double, char, boolean in uppercase, e.g., INT, DOUBLE, CHAR, etc. There are certain classes in Java that are somewhat equivalent to these simple types, so Double and Integer won’t trigger a syntax error, but the code won’t behave as expected.
Incorrectly writing the names of predefined complex types String, LocalDateTime in lowercase, e.g., string, localdatetime, etc. These are classes, so the naming convention for classes applies: the first letter of each word is uppercase, and all words are connected.
Forgetting to write a semicolon (;) at the end of each command.
Writing char values with double quotation marks instead of apostrophes (e.g., “W” instead of ‘W’).
Writing String values with apostrophes instead of double quotation marks (e.g., ‘sun’ instead of “sun”).
Including a space in the attribute name, e.g., writing processor Speed instead of processorSpeed.
Other common errors that aren’t syntax errors, but affect the program’s functionality, include:
- Using real data types (float, double) in situations where an integer data type is appropriate (int, short, long). For example, the number of students is an integer, even though someone may declare it as a real number. Although this is not a syntax error, it may cause issues later because some operators in Java work only with integers.
Time for tasks
Now that you have read the theory lesson, it is time for tasks. If you haven’t already, install IntelliJ with the Jetbrains Academy plugin, and load the Interactive Java workbook part 1. It is contained in the “Interactive Java workbook 1.zip” file accompanying this book.
Installation and introductory video tutorials can be found in this playlist for convenience.
Happy coding!!!