JAVA PROGRAMMING Leksioni 1
JAVA PROGRAMMING Leksioni 1
This is not an easy concept. A computer is a machine built of millions of tiny switches called transistors, which have
the property that can be wired together in such a way that an output from one switch can turn another switch on or off.
What really happening is that JAVA uses just-in-time compilers for executing Java bytecode. A just-in-time compiler
translates Java bytecode into native machine language. It does this while it is executing the program. Just as for a
normal interpreter, the input to a just-in-time compiler is a Java bytecode program, and its task is to execute that
program. But as it is executing the program, it also translates parts of it into machine language. This is platform-
independence and even more exciting is that it provides Graphical User Interface.
Now we get to programming basics
We will see (as usual) the Hello World program
The programming language is made of:
• Syntax (rules how this language is written)
• Semantics (the meaning of the program)
• Pragmatics (the style how the code is written)
The word "public" in the first line of main() means that this routine can be called from outside the program. This is
essential because the main() routine is called by the Java interpreter, which is something external to the program itself.
The remainder of the first line of the routine is harder to explain at the moment; for now, just think of it as part of the
required syntax.
Now we get to programming basics
Statements
When you tell the Java interpreter to run the program, the interpreter calls this main() subroutine, and the statements
that it contains are executed. These statements make up the script that tells the computer exactly what to do when the
program is executed.
In this case when you run this program, the message "Hello World!" (without the quotes) will be displayed on standard
output. It uses a "built-in subroutine" named System.out.println to do the actual work. A subroutine consists of the
instructions for performing some task, chunked together and given a name.
Control Structures
Control structures are special instructions that can change the flow of control. There are two basic types of
control structure: loops, which allow a sequence of instructions to be repeated over and over, and branches,
which allow the computer to decide between two or more different courses of action by testing conditions that
occur as the program is running.
Now we get to programming basics
About variables
A variable is just a memory location (or several consecutive locations treated as a unit) that has been given a
name so that it can be easily referred to and used in a program. The programmer only has to worry about the
name; it is the compiler's responsibility to keep track of the memory location. As a programmer, you just need to
keep in mind that the name refers to a kind of "box" in memory that can hold data, even though you don't have to
know where in memory that box is located.
• The first four types hold integers (whole numbers such as 17, -38477, and 0). The four integer types are
distinguished by the ranges of integers they can hold.
• The float and double types hold real numbers (such as 3.6 and -145.99
• A variable of type char holds a single character from the Unicode character set.
• And a variable of type boolean holds one of the two logical values true or false.
Now we get to programming basics
We dig a bit deeper into variables
• short corresponds to two bytes (16 bits). Variables of type short have values in the range -32768 to 32767.
• int corresponds to four bytes (32 bits). Variables of type int have values in the range -2147483648 to
2147483647.
• long corresponds to eight bytes (64 bits). Variables of type long have values in the range -
9223372036854775808 to 9223372036854775807.
• float data type is represented in four bytes of memory, using a standard method for encoding real numbers. The
maximum value for a float is about 10 raised to the power 38.
• double takes up 8 bytes, can range up to about 10 to the power 308, and has about 15 significant digits.
• char occupies two bytes in memory. The value of a char variable is a single character such as A, *, x, or a
space character. The value can also be a special character such a tab or a carriage return or one of the many
Unicode characters that come from different languages. A char value is written inside single quotes ‘ ’. Like: a
= ‘&’;
Now we get to programming basics
How we work with variables
A variable can be used in a program only if it has
first been declared. A variable declaration statement
is used to declare one or more variables and to give
them names. When the computer executes a
variable declaration, it sets aside memory for the
variable and associates the variable's name with that
memory.
Variables declared inside a subroutine are called
local variables for that subroutine.
For the functions s1.toUpperCase(), s1.toLowerCase(), and s1.trim(), note that the value of s1 is not changed.
Instead a new string is created and returned as the value of the function. The returned value could be used, for
example, in an assignment statement such as "smallLetters = s1.toLowerCase();". To change the value of s1, you
could use an assignment "s1 = s1.toLowerCase();".
Now we get to programming basics
Did we talk about System.out.print() yet?
The most basic output function is System.out.print(x), where x can be a value or expression of any type. If the
parameter, x, is not already a string, it is converted to a value of type String, and the string is then output to the
destination called standard output.
System.out.println(x) outputs the same text as System.out.print, but it follows that text by a line feed, which means
that any subsequent output will be on the next line.
Formatted output?
You might have noticed that System.out.print outputs real numbers with as many digits after the decimal point as
necessary, so that for example π is output as 3.141592653589793. You might prefer to have these numbers output
as, for example, 3.14159.
That’s why JAVA has a formatted output.
Now we get to programming basics
Format Specifiers
Every format specifier begins with a percent sign (%) and ends with a letter, possibly with some extra formatting
information in between. The letter specifies the type of output that is to be produced.
For example, in %d and %12d, the "d" specifies that an integer is to be written. The "12" in %12d specifies the
minimum number of spaces that should be used for the output. If the integer that is being output takes up fewer
than 12 spaces, extra blank spaces are added in front of the integer to bring the total up to 12. We say that the
output is "right-justified in a field of length 12." A very large value is not forced into 12 spaces; if the value has
more than 12 digits, all the digits will be printed, with no extra spaces.
Type double are just a bit more complicated. An "f", as in %1.2f, is used to output a number in "floating-point"
form, that is with digits after a decimal point. In %1.2f, the "2" specifies the number of digits to use after the
decimal point. The "1" specifies the (minimum) number of characters to output; a "1" in this position effectively
means that just as many characters as are necessary should be used. Similarly, %12.3f would specify a floating-
point format with 3 digits after the decimal point, right-justified in a field of length 12.
Now we get to programming basics
The whole list
Next Lesson
We are nearly there! True programmers be ready!
In the next lesson we will cover:
• Literals
• Arithmetic Operators
• Increment & Decrement
• Relational Operators
• Boolean Operators
• Conditional Operators
• Assignment Operators and Precedence Rules
• Text Input and IDE