A Sample of Java
A Sample of Java
Let's create the classic "Hello, World!" program, a friendly greeting from your
computer.
Don't worry if it looks a bit cryptic now. You'll get the hang of it soon.
This program simply prints the phrase "Hello, World!" to the console. But
there's a lot going on here:
And there you have it, your first Java program! It's a modest step, but it
marks the beginning of an exciting journey into Java programming.
Variables can only store matching data. You wouldn't want to accidentally put honey
in a cardboard cereal box or pour cereal into a salt shaker. To prevent such mistakes,
learn to distinguish between the basic literals: integer numbers, strings, and characters.
Integer numbers
You use these numbers to count things in the real world as natural numbers.
Integer numbers also include zero and negative ones. Here are several
examples of valid integer number literals separated by commas: 0, 1, 2, 10,
11, -100.
Reading code is crucial for anyone in IT, so let's parse it together. Here you
put the integer 1000 into a variable of an integer type, called numApples.
This is similar to filling a container with its designated contents!
You can increase code readability by dividing the digit into blocks
with underscores: 1_000_000 is more readable than 1000000. So let's pack our
apples to make selling them easier:
int numPackedApples = 1_000_000;
Fear not if these code snippets aren't 100% clear to you yet! They aim to help
you develop the skill of code reading. Just grasp the overall meaning and
follow your study plan, and you'll be writing your own code in no time!
Characters
A character is a single symbol, denoted with single quotes. You can
use character literals to represent single letters like 'A', 'x', digits
from '0' to '9', whitespaces (' '), and other characters or symbols like '$'.
Fun fact: characters sit between integers and strings: they resemble strings,
yet you can do math with them
Strings
A string is a sequence of characters, encapsulated by double quotes. It
represents text-based information, such as an advertising line, a webpage
address, or a website login name. Here are some valid examples: "text", "I
want to know Java", "123456", "e-mail@gmail.com" . As you can see, a string can
include letters, digits, whitespaces, and other charactes altogether.
A string consisting of a single character like "A" is also a valid string, but do
not confuse it with the 'A' character. Note the difference in quotes!
char singleQuoted = 'A'
String doubleQuoted = "A"
Conclusion
You have learned to distinguish between the following literals:
n 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.
In Java, the newline character is a special character that signifies the end of a line and the
beginning of a new one.
Read more
Was this information helpful?
YesNoReport
You can type this code in the Your Code section here and then press
the execute button. In the result section, you will see:
Hello, World!
If you have already installed Java, you can run the program on your computer. If not,
there is no need to install it right now. We will do that later.
1. The public class. It is the basic unit of a program. Every Java program
must have at least one class. The definition of a class consists of
the class keyword followed by the class name. A class can have any name,
such as App, Main, or Program, but it must not start with a digit. A set of
braces {...} encloses the body of a class.
public class Main {
// ...
}
The text after // is just a comment, not a part of the program. We will learn
about comments in detail in later topics.
This is one of the most important things to understand from the Hello
World program. We invoke a special method println to display a string
followed by a new line on the screen. We will often use this approach to print
something of interest to the screen. The text is printed without double
quotes.
Keywords
As you can see, even a simple Java program consists of many elements,
including keywords that are parts of the language. In total, Java provides more than
50 keywords which you will gradually learn on this platform. The full list is here,
though you don't need to remember all of them at this moment.
Conclusion
We have discussed the simplest program you can write in Java. It has a single class
with a single main method. Every Java program must have a main method as it is the
first to be executed when the program runs. Don't worry about memorizing every
single term used in the topic (syntax, statement, block). These terms will reappear in
further materials. Do not forget to use the provided Hello World program as a
template in your own programs.
When you write programs, you often need to print calculation results, text, or any
other type of data. Also, throughout this educational platform, you will write a lot of
programs that print data on the screen. Let's learn how to do that using a standard
approach in Java.
The println method displays the passed string followed by a new line on the screen
(print-line). For example, the following code snippet prints four lines.
System.out.println("I ");
System.out.println("know ");
System.out.println("Java ");
System.out.println("well.");
I
know
Java
well.
All the strings were printed as they are, without double quotes.
This method allows you to print an empty line when no string is given:
System.out.print("I ");
System.out.print("know ");
System.out.print("Java ");
System.out.print("well.");
Pay attention to the spaces between words. We pass them to the method for printing.
4 letters sout and pressing the Tab key in the IntelliJ IDEA development environment
will automatically write System.out.println() for you
System.out.print(22);
System.out.print('E');
System.out.print(8);
System.out.println('1');
108cQ3
22E81
As is the case with strings, none of the printed characters contain quotes.
Conclusion
In Java, you can print data via the standard output using the System.out object. You
can use the println method to display the passed string in a print-line and
the print method to output all passed strings in a single line. Both of these methods
also allow for printing numbers as well as characters.