0% found this document useful (0 votes)
28 views25 pages

JAVA PROGRAMMING Leksioni 1

The document provides an introduction to Java programming, starting from basic concepts like what a program and computer language are. It discusses how a computer executes a program, memory, the CPU, and machine language. It also covers Java specifics like just-in-time compilation and virtual machines. Key programming concepts like variables, data types, and control structures are explained.

Uploaded by

femefir665
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views25 pages

JAVA PROGRAMMING Leksioni 1

The document provides an introduction to Java programming, starting from basic concepts like what a program and computer language are. It discusses how a computer executes a program, memory, the CPU, and machine language. It also covers Java specifics like just-in-time compilation and virtual machines. Key programming concepts like variables, data types, and control structures are explained.

Uploaded by

femefir665
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

JAVA PROGRAMMING

Starting from the basics


Let’s start from the basics and create a roadmap
What is a program?
A program is simply a list of instructions meant to be followed mechanically by a computer.

What language does the computer understand?


A computer is built to carry out instructions that are written in a very simple type of language called machine language.

How does machine language look like?


Well it looks like this
And the computer can directly execute a program only if the program is
expressed in that language. Also, it can execute programs written in other
languages if they are first translated into machine language.
Let’s start from the basics and create a roadmap
But how does the computer execute the program?
First, the program is stored in the computer's main memory which also holds data that is being used or processed by
the program. Main memory consists of a sequence of locations. These locations are numbered, and the sequence
number of a location is called its address.

How does the memory look like?


The memory looks like this:

And we represent it like this:


Let’s start from the basics and create a roadmap
Still haven’t told us how the computer executes the program.
The heart—or the brain, if you want—of the computer is a single component called the Central Processing Unit that
does the actual computing. A CPU contains an Arithmetic Logic Unit, or ALU, which is the part of the processor that
carries out operations such as addition and subtraction. The CPU also includes special purpose registers. The most
important of these is the program counter, or PC to keep track of where it is in the program it is executing.

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.

How does the CPU look like?


The CPU looks like this:
Let’s start from the basics and create a roadmap
Let’s recap what we learned in one diagram
Let’s start from the basics and create a roadmap
So… What does all this has to do with JAVA or Programming in general?
Almost all programs, though, are written in high-level programming languages such as Java, Python, or C++. A
program written in a high-level language cannot be run directly on any computer. First, it has to be translated into
machine language. This translation can be done by a program called a compiler. If the program is to run on another
type of computer it has to be re-translated, using a different compiler, into the appropriate machine language.

And here is where JAVA stands out from the rest


The designers of Java chose to use a combination of compiling and interpreting (which translates the program
instruction-by-instruction). Programs written in Java are compiled into machine language, but it is a machine language
for a computer that doesn't really exist.
This so-called "virtual" computer is known as the Java Virtual Machine, or JVM. The machine language for the Java
Virtual Machine is called Java bytecode. There is no reason why Java bytecode couldn't be used as the machine
language of a real computer, rather than a virtual computer. But in fact the use of a virtual machine makes possible one
of the main selling points of Java: the fact that it can actually be used on any computer.
Let’s start from the basics and create a roadmap
By now, you know we like diagrams and pics, so here is one more

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)

Can you identify them in the example on the left?

What else can we see in this program?


We see an example of comments, a class, main routine & some arguments and a statement.
Now we get to programming basics
Comments
Comments in a program are entirely ignored by the computer; they are there for human readers only.
The first type begins with // and extends to the end of a line. There is a comment of this form on the last line of the
above program. The computer ignores the // and everything that follows it on the same line. The second type of
comment starts with /* and ends with */, and it can extend over more than one line.

The first public class


The program-name in the line that begins "public class" is the name of the program, as well as the name of the class.
(Remember, again, that program-name is a placeholder for the actual name!) If the name of the class is HelloWorld,
then the class must be saved in a file called HelloWorld.java.
When this file is compiled, another file named HelloWorld.class will be produced. This class file, HelloWorld.class,
contains the translation of the program into Java bytecode
Now we get to programming basics
Main Routine
When you tell the Java interpreter to run the program, the interpreter calls this main() routine, 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. The main() routine can call other subroutines that are defined in the same class or even in other
classes, but it is the main() routine that determines how and in what order the other subroutines are used.

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.

But can we make a blueprint of any program?


Now we get to programming basics
Yes we can. And here it is…
The first two lines have to do with using packages. A
package is a group of classes. We will learn about those
later.

So now we can build any type of program?


Now we get to programming basics
Sure. Just a few more things we have to understand.
To make a program, usually we work with data. Then we make instructions.
To work with data, you need to understand variables and types; to work with instructions, you need to
understand control structures and subroutines.
We talked about a bit about subroutines, but what are those other two?

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.

We can manipulate the data like this


Now we get to programming basics
To make the computer life easier, we have data types
A variable in Java is designed to hold only one particular type of data; it can legally hold that type of data and no
other. The compiler will consider it to be a syntax error if you try to violate this rule by assigning a value of the
wrong type to a variable. We say that Java is a strongly typed language because it enforces this rule.
There are eight so-called primitive types built into Java. The primitive types are named byte, short, int, long,
float, double, char, and boolean.

• 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.

Let’s see an example


Now we get to programming basics
Okay, but how do we work with text?
Java has other types in addition to the primitive types, but all the other types represent objects rather than
"primitive" data values. For the most part, we are not concerned with objects for the time being. However, there
is one predefined object type that is very important: the type String.
A value of type String is a sequence of characters. You've already seen a string literal: "Hello World!". The
double quotes are part of the literal; they have to be typed in the program.
A string can contain any number of characters, even zero. A string with no characters is called the empty string
and is represented by the literal "", a pair of double quote marks with nothing between them. Remember the
difference between single quotes and double quotes! Single quotes are used for char literals and double quotes
for String literals! There is a big difference between the String "A" and the char 'A’.
Let’s make a String declaration and assign a value:
Now we get to programming basics
Operations on Strings
String is a class, and a value of type String is an object. That object contains data, namely the sequence of
characters that make up the string (don’t worry about these now). It also contains subroutines. All of these
subroutines are in fact functions. Let’s assume that s1 and s2 are variables of type String:
• s1.equals(s2) is a function that returns a boolean value. It returns true if s1 consists of exactly the same
sequence of characters as s2, and returns false otherwise.
• s1.equalsIgnoreCase(s2) is another boolean-valued function that checks whether s1 is the same string as s2, but
this function considers upper and lower case letters to be equivalent. Thus, if s1 is "cat", then s1.equals("Cat") is
false, while s1.equalsIgnoreCase("Cat") is true.
• s1.length(), as mentioned above, is an integer-valued function that gives the number of characters in s1.
• s1.charAt(N), where N is an integer, returns a value of type char. It returns the Nth character in the string.
Positions are numbered starting with 0, so s1.charAt(0) is actually the first character, s1.charAt(1) is the second,
and so on. The final position is s1.length() - 1. For example, the value of "cat".charAt(1) is 'a'. An error occurs if
the value of the parameter is less than zero or is greater than or equal to s1.length().
Now we get to programming basics
Operations on Strings
• s1.substring(N,M), where N and M are integers, returns a value of type String. The returned value consists of
the characters of s1 in positions N, N+1,..., M-1. Note that the character in position M is not included. The
returned value is called a substring of s1. The subroutine s1.substring(N) returns the substring of s1 consisting
of characters starting at position N up until the end of the string.
• s1.indexOf(s2) returns an integer. If s2 occurs as a substring of s1, then the returned value is the starting
position of that substring. Otherwise, the returned value is -1. You can also use s1.indexOf(ch) to search for a
char, ch, in s1. To find the first occurrence of x at or after position N, you can use s1.indexOf(x,N). To find the
last occurrence of x in s1, use s1.lastIndexOf(x).
• s1.compareTo(s2) is an integer-valued function that compares the two strings. If the strings are equal, the value
returned is zero. If s1 is less than s2, the value returned is a number less than zero, and if s1 is greater than s2,
the value returned is some number greater than zero. There is also a function s1.compareToIgnoreCase(s2). (If
both of the strings consist entirely of lower case letters, or if they consist entirely of upper case letters, then
"less than" and "greater than" refer to alphabetical order. Otherwise, the ordering is more complicated.)
Now we get to programming basics
Operations on Strings
• s1.toUpperCase() is a String-valued function that returns a new string that is equal to s1, except that any lower
case letters in s1 have been converted to upper case. For example, "Cat".toUpperCase() is the string "CAT".
There is also a function s1.toLowerCase().
• s1.trim() is a String-valued function that returns a new string that is equal to s1 except that any non-printing
characters such as spaces and tabs have been trimmed from the beginning and from the end of the string. Thus,
if s1 has the value "fred ", then s1.trim() is the string "fred", with the spaces at the end removed.

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

You might also like