Posts

Naming variables

Why is naming important? As you may know, every variable has a name that uniquely identifies it among other variables. Giving a good name to a variable may not be as simple as it seems. Experienced programmers put a lot of care into naming to make their programs easy to understand. It is important because programmers spend much time on reading and understanding code written by other programmers. If variables have bad names, even your own code will seem unclear to you in a few months. Always try to give descriptive and concise names to all variables. As a result, any programmer will enjoy your code for a long time. In addition, there are two sets of rules that restrict the possible names for variables. Rules for naming variables Java has some rules for naming variables: ⏭ Names are case-sensitive; ⏭ A name can include Unicode letters, digits, and two special characters ($, _); ⏭ A name cannot start with a digit; ⏭ A name must not be a keyword (class, static, int are illegal names). Base...

Comments

Inside a Java program, you can write a special text that will be ignored by the java compiler — known as the comment. Comments allow you to exclude code from the compilation process (disable it) or clarify a piece of code to yourself or other developers. In our materials, we use comments in the theory and practice lessons to explain how and why our code works. The Java programming language supports three kinds of comments. End-of-line comments The java compiler ignores any text from // to the end of the line. class Program {     public static void main(String[] args) {         // The line below will be ignored         // System.out.println("Hello, World");         // It prints the string "Hello, Java"         System.out.println("Hello, Java"); // Here can be any comment     } } In the example above the text after // is ignored by the compiler. Multi-line comments The compiler ignores any ...

Accessing the value of a variable

Once a variable has been declared, its value can be accessed and modified using the name. In the example below, we declare a variable and then print it: String dayOfWeek = "Monday"; System.out.println(dayOfWeek); // Monday It is also possible to assign a value of one variable to another one: int one = 1; int num = one; System.out.println(num); // 1 One important feature of variables is that they can be changed. You don't need to declare a variable again to change its value; just assign a new value to it using the = operator. Let's declare a variable named dayOfWeek and print its value before and after changing: String dayOfWeek = "Monday"; System.out.println(dayOfWeek); // Monday   dayOfWeek = "Tuesday"; System.out.println(dayOfWeek); // Tuesday There is one restriction for variables: you can only assign a value of the same type as the type of the initial variable. So, the following code is not correct: int number = 10; number = 11; // ok number = ...

Types and variables

Declaring and initializing A variable is a placeholder for storing a value of a particular type: a string, a number, or something else. Every variable has a name (also known as an identifier) to distinguish it from others. Before we start using a variable, we must declare it. The general form of declaration is the following: DataType variableName = initialization; The left part of this statement describes the variable, and the right part describes something that is assigned to it. The type (or data type) of a variable determines what possible operations can be performed on the variable and which values can be stored in it. Here we use a non-existing data type (DataType) to demonstrate the general form of declaration. The name (or identifier) distinguishes the variable from others. The name of a variable cannot start with a digit; it usually starts with a letter. Always try to choose meaningful and readable names for variables to make your code easy to understand. The assignment operato...

Displaying text using println() and print()

Standard output is a receiver to which a program can send information (text). It is supported by all common operating systems. Java provides a special object System.out to work with the standard output. We will often use it to print something. The println method displays the passed string followed by a new line on the screen (print-line). As an example, the following code fragment prints four lines. System.out.println("I "); System.out.println("know "); System.out.println("Java "); System.out.println("well."); The output: I know Java well. All strings were printed as is, without the double quotes. The method allows you to print the empty line when no string is given: System.out.println("Java is a popular programming language."); System.out.println(); // prints empty line System.out.println("It is used all over the world!"); The output: Java is a popular programming language.   It is used all over the world! The print method di...

Overview of the basic program

In this topic, we will build our very first Java program. Our program will simply print "Hello, World!" on the screen (a tradition by most programmers when learning new languages). Our code may not seem too exciting at first, however, we will learn about the basic template that all Java programs need to follow. The Hello World program public class Main {     public static void main(String[] args) {         System.out.println("Hello, World!");     } }  The result will be: Hello, World! The basic terminology Now that we have seen the result, let's learn some basic terminology and then try to understand this program. Program – a sequence of instructions (called statements), which are executed one after another in a predictable manner. Sequential flow is the most common and straightforward sequence of statements, in which statements are executed in the order that they are written – from top to bottom in a sequential manner; Statement – a single...
Literals Regardless of its complexity, a program always performs operations on numbers, strings, and other values. These values are called literals. There are many different sorts of literals in Java, but in this topic we will focus only on a few of them: the ones that surround us all the time in everyday life. Let's consider integer numbers, strings, and characters in the format in which they are written in Java. Integer numbers These numbers are used to count things in the real world. Also, we will often use them in Java. Here are several examples of valid integer number literals separated by commas: 0, 1, 2, 10, 11, 100. If an integer value contains a lot of digits, we can add underscores to divide the digit into blocks for increased readability: 1_000_000. It's more readable than the same value written as 1000000. Characters A single character can represent a digit, a letter or another symbol. To write a character we use single quotes as follows: 'A', 'B', ...