Java Program Sololearrn
Java Program Sololearrn
Let's start by creating a simple program that prints “Hello World” to the screen.
class MyClass {
public static void main(String[ ] args) {
System.out.println("Hello World");
}
}Try It Yourself
In Java, every line of code that can actually run needs to be inside a class.
In our example, we named the class MyClass. You will learn more about classes in the upcoming
modules.
In Java, each application has an entry point, or a starting point, which is a method called main. Along
with main, the keywords public and static will also be explained later.
As a summary:
- Every program in Java must have a class.
- Every Java program starts from the main method.
For example, the following code declares a method called test, which does not return anything and has
no parameters:void test()
The method's parameters are declared inside the parentheses that follow the name of the method.
For main, it's an array of strings called args. We will use it in our next lesson, so don't worry if you
don't understand it all now.
System.out.println()
{
System.out.println("Hello World!");
}
The println method prints a line of text to the screen.
The System class and its out stream are used to access the println method.
In classes, methods, and other flow-control structures code is always enclosed in curly braces { }.
Semicolons in Java
You can pass a different text as the parameter to the println method to print it.
class MyClass {
public static void main(String[ ] args) {
System.out.println("I am learning Java");
}
}Try It Yourself
In Java, each code statement must end with a semicolon.
Remember: do not use semicolons after method and class declarations that follow with the body
defined using the curly braces.
Apples {
Comments
The purpose of including comments in your code is to explain what the code is doing.
Java supports both single and multi-line comments. All characters that appear within a comment are
ignored by the Java compiler.
A single-line comment starts with two forward slashes and continues until it reaches the end of the
line.
For example:
Adding comments as you write code is a good practice, because they provide clarification and
understanding when you need to refer back to it, as well as for others who might need to read it.
Multi-Line Comments
Java also supports comments that span multiple lines.
You start this type of comment with a forward slash followed by an asterisk, and end it with an
asterisk followed by a forward slash.
For example:
/* This is also a
comment spanning
multiple lines */
Documentation Comments
Documentation comments are special comments that have the appearance of multi-line comments,
with the difference being that they generate external documentation of your source code. These begin
with a forward slash followed by two asterisks, and end with an asterisk followed by a forward slash.
For example:
Variables
This creates a variable called name of type String, and assigns it the value "David".
It is important to note that a variable is associated with a type, and is only capable of storing values of
that particular type. For example, an int variable can store integervalues, such as 123; but it cannot
store real numbers, such as 12.34, or texts, such as "Hello".
Variables
class MyClass {
public static void main(String[ ] args) {
String name ="David";
int age = 42;
double score =15.9;
char group = 'Z';
}
}
Another type is the Boolean type, which has only two possible values: true and false.
This data type is used for simple flags that track true/false conditions.
For example:
You can use a comma-separated list to declare more than one variable of the specified type.
Example: int a = 42, b = 11;
Drag and drop from the options below to have a valid Java program.
class Apples {
public static void main(String[ ]args) {
____name = "John";
___age = 24;
___height = 189.87;
}
}
int,void,class,true,double,String
The Math Operators
Java provides a rich set of operators to use in manipulating variables. A value used on either side of an
operator is called an operand.
For example, in the expression below, the numbers 6 and 3 are operands of the plus operator:
int x = 6 + 3;
Addition
The + operator adds together two values, such as two constants, a constant and a variable, or a variable
and a variable. Here are a few examples of addition:
Subtraction
Just like in algebra, you can use both of the operations in a single line. For example: intval = 10 + 5 -
2;
int x = 2; int y = 4;
int result = x _ _ ;
System.out.println(___);
Multiplication
Division
Modulo
The modulo (or remainder) math operation performs an integer division of one value by another, and
returns the remainder of that division.
The operator for the modulo operation is the percentage (%) character.
Example:
An increment or decrement operator provides a more convenient and compact way to increase or
decrease the value of a variable by one.
For example, the statement x=x+1; can be simplified to ++x;
Example:
int test = 5;
++test; // test is now 6
The decrement operator (--) is used to decrease the value of a variable by one.
int test = 5;
--test; // test is now 4
int a = 10;
_ a;
System.out.println(a);
Prefix & Postfix
Two forms, prefix and postfix, may be used with both the increment and decrement operators.
With prefix form, the operator appears before the operand, while in postfix form, the operator appears
after the operand. Below is an explanation of how the two forms work:
Prefix: Increments the variable's value and uses the new value in the expression.
Example:
int x = 34;
int y = ++x; // y is 35
The value of x is first incremented to 35, and is then assigned to y, so the values of both x and y are
now 35.
Postfix: The variable's value is first used in the expression and is then increased.
Example:
int x = 34;
int y = x++; // y is 34
x is first assigned to y, and is then incremented by one. Therefore, x becomes 35, while y is assigned
the value of 34.
The same applies to the decrement operator.
You are already familiar with the assignment operator (=), which assigns a value to a variable.int
value = 5;
This assigned the value 5 to a variable called value of type int.
int num1 = 4;
int num2 = 8;
num2 += num1; // num2 = num2 + num1;
int num1 = 4;
int num2 = 8;
num2 -= num1; // num2 = num2 - num1;
Similarly, Java supports multiplication and assignment (*=), division and assignment (/=), and
remainder and assignment (%=).
y = _ - 12;
System.out.println( _ );
Strings
For example:
String s = "SoloLearn";
You are allowed to define an empty string. For example, String str = "";
Drag and drop from the options below to print "Hello".
___var;
var = "Hello";
System.out.println(____);
String char var print Hello
String Concatenation
The + (plus) operator between strings adds them together to make a new string. This process is
called concatenation.
The resulted string is the first string put together with the second string.
For example:
While Java provides many different methods for getting user input, the Scanner object is the most
common, and perhaps the easiest to implement. Import the Scanner class to use the Scanner object,
as seen here:
import java.util.Scanner;
In order to use the Scanner class, create an instance of the class by using the following syntax:
You can now read in different kinds of input data that the user enters.
Here are some methods that are available through the Scanner class:
Read a byte - nextByte()
Read a short - nextShort()
Read an int - nextInt()
Read a long - nextLong()
Read a float - nextFloat()
Read a double - nextDouble()
Read a boolean - nextBoolean()
Read a complete line - nextLine()
Read a word - next()
import java.util.Scanner;
class MyClass {
public static void main(String[ ] args) {
Scanner myVar = new Scanner(System.in);
System.out.println(myVar.nextLine());
}
}
This will wait for the user to input something and print that input.
The code might seem complex, but you will understand it all in the upcoming lessons.
Drag and drop from the options below to get user input.
import java.util.Scanner;
class test {
public static void main(String[ ] args) {
Quiz
Please type in a code to declare two variables of type int and print their sum using
the sum variable.
int x = 4;
__ y=7;
int sum = x _ y ;
System.out.println(___ );
Drag and drop from the options below to output the name:
____ name;
name = "David";