Basic Concepts

 

Chapter: 1

Introduction to Java


Welcome to Java!


Java is a very popular programming language that runs on billions of devices, including computers, mobile devices, gaming consoles, medical devices, and many others. It's widely used in web, mobile, and game development, desktop applications, banking, testing, and more.

Java is platform-independent, which means that you only need to write the program once to be able to run it on a number of different platforms and devices!
Java guarantees that you'll be able to Write Once, Run Anywhere.

Welcome to Java!

Write once, run everywhere. This means that

Java suggests to to do cardio exercise more often
Java is platform-independent
You need to write a separate code for each platform

Your First Java Program


Let's start by creating a simple program that sends a message to the screen or display device we want to program:
class MyClass {
public static void main(String[ ] args) {
System.out.println("60");
}
}
JAVA
contentImage
You'll learn what each of the statements does in the upcoming lessons.
For now, remember that:
- Every program in Java must have a class (MyClass in the example).
- Every Java program starts execution from the main method.

Your First Java Program

The starting point of every Java program is

any method
the main method
the first line of the code

System.out.println()


Next is the body of the main method is enclosed in curly braces:
{
System.out.println("I'm learning Java!");
}
JAVA
The System.out.println() method prints or sends a line of text to the screen.
In classes, methods, and other flow-control structures code is always enclosed in curly braces { }.

System.out.println()

Which method prints a line of text to the screen?

log()
printText()
System.out.println()

Semicolons in Java


You can change the text in the println method to print a different message:
class MyClass {
public static void main(String[ ] args) {
System.out.println("Your message here");
}
}
JAVA
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.

Semicolons in Java

Drag and drop to create a valid Java program.

class MyClass {


public static void

main

(String[ ] args) {


System.out.

println

("That's easy!");


}


}



Lesson Takeaways


Let's summarize what we've just learned:
Java is one the most popular programming languages in the world
- Java is platform-independent, which means that you only need to write the program once to be able to run it on a number of different platforms and devices
- you can use System.out.println() method to send text or numbers to the screen
- In Java, each code statement must end with a semicolon.

Lesson Takeaways

Drag and drop to fill in the blanks and output a message.

System.

out

.println

(

"I've finished my first Java lesson!")

;

Your First Java Program


Q. Complete the given program to print "Java is fun".
Note that the sentence starts with a capital letter.

Hint
Just add the text to the System.out.println() method
Remember to enclose the text into double quotes.

Chapter: 2

Java Comments


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.

single-line comment starts with two forward slashes and continues until it reaches the end of the line.
For example:
// this is a single-line comment
x = 5; // a single-line comment after code
JAVA
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.

Comments

Single-line comments are created using:

// characters at the end of the line
*/ characters at the beginning of the line
** characters at the beginning of the line
// characters at the beginning of the line

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 */
JAVA
Note that Java does not support nested multi-line comments.
However, you can nest single-line comments within multi-line comments.
/* This is a single-line comment:

// a single-line comment

*/
JAVA
Another name for a Multi-Line comment is a Block comment.

Multi-Line Comments

Make this text a multi-line comment.

some

* comment text


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:
/** This is a documentation comment */

/** This is also a
documentation comment */
JAVA
Javadoc is a tool which comes with JDK and it is used for generating Java code documentation in HTML format from Java source code which has required documentation in a predefined format.

When a documentation comment begins with more than two asterisks, Javadoc assumes that you want to create a "box" around the comment in the source code. It simply ignores the extra asterisks.
For example:
/**********************

This is the start of a method

***********************/
JAVA
This will retain just the text "This is the start of a method" for the documentation.

Documentation Comments

You can add a Java doc style comment by using:

// characters in the beginning of the line.
/* and */ characters to wrap a comment.
/** and */ characters to wrap a comment.
// and */ characters to wrap a comment.

Chapter: 3

Variables


Variables


Variables store data for processing.
A variable is given a name (or identifier), such as area, age, height, and the like. The name uniquely identifies each variable, assigning a value to the variable and retrieving the value stored.

Variables have types. Some examples:
int: for integers (whole numbers) such as 123 and -456
double: for floating-point or real numbers with optional decimal points and fractional parts in fixed or scientific notations, such as 3.1416, -55.66.
String: for texts such as "Hello" or "Good Morning!". Text strings are enclosed within double quotes.

You can declare a variable of a type and assign it a value.
Example:
String name = "David";
JAVA
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 integer values, such as 123; but it cannot store real numbers, such as 12.34, or texts, such as "Hello".

Variables

Which variable type would you use for a city name?

double
String
int

Variables


Examples of variable declarations:
class MyClass {
public static void main(String[ ] args) {
String name ="David";
int age = 42;
double score =15.9;
char group = 'Z';
}
}
JAVA
char stands for character and holds a single character.

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:
boolean online = true;
JAVA
You can use a comma-separated list to declare more than one variable of the specified type. Example: int a = 42, b = 11;

Variables

Drag and drop from the options below to have a valid Java program.

class Apples {


public static void main(String[ ]args) {


String

name = "John";


int

age = 24;


double

height = 189.87;


}


}


Chapter: 4

Getting User Input


Getting User Input


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;
JAVA
In order to use the Scanner class, create an instance of the class by using the following syntax:
Scanner myVar = new Scanner(System.in);
JAVA
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()

Example of a program used to get user input:
import java.util.Scanner;

class MyClass {
public static void main(String[ ] args) {
Scanner myVar = new Scanner(System.in);
System.out.println(myVar.nextLine());
}
}
JAVA
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.

Getting User Input

Drag and drop from the options below to get user input.

import java.util.Scanner;


class test {


public static void main(String[ ] args) {


Scanner

sc = new Scanner(System.in);


String st = sc.

nextLine

();


}


}


Chapter: 5

Primitive Operators


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;
JAVA
Java arithmetic operators:
addition
subtraction
multiplication
division
modulo
Arithmetic operators are used in mathematical expressions in the same way that they are used in algebraic equations.

The Math Operators

Fill in the blank to declare an integer variable and set its value to 5.

var =
;


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:
int sum1 = 50 + 10;
int sum2 = sum1 + 66;
int sum3 = sum2 + sum2;
JAVA

Substraction


The - operator subtracts one value from another.
int sum1 = 1000 - 10;
int sum2 = sum1 - 5;
int sum3 = sum1 - sum2;
JAVA
Just like in algebra, you can use both of the operations in a single line. For example: int val = 10 + 5 - 2;

Addition

Fill in the blanks to print the sum of the two variables.

int x = 2; int y = 4;

int result = x
;

System.out.println(
);

Multiplication


The * operator multiplies two values.
int sum1 = 1000 * 2;
int sum2 = sum1 * 10;
int sum3 = sum1 * sum2;
JAVA

Division


The / operator divides one value by another.
int sum1 = 1000 / 5;
int sum2 = sum1 / 2;
int sum3 = sum1 / sum2;
JAVA
In the example above, the result of the division equation will be a whole number, as int is used as the data type. You can use double to retrieve a value with a decimal point.

Multiplication

What is the result of the following code? int x = 15; int y = 4; int result = x / y; System.out.println(result);


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:
int value = 23;
int res = value % 6; // res is 5
JAVA
Dividing 23 by 6 returns a quotient of 3, with a remainder of 5. Thus, the value of 5 is assigned to the res variable.

Modulo

What value is stored in the result variable? int x = 8, y = 5; int result = x % y;


Chapter: 6

Incriment and Decriment


Increment Operators


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
JAVA
The decrement operator (--) is used to decrease the value of a variable by one.
int test = 5;
--test; // test is now 4
JAVA
Use this operator with caution to avoid calculation mistakes.

Increment Operators

Fill in the blanks to print 11.

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

Prefix & Postfix

What is the output of the following code? int x = 14; System.out.println(x++);



Assignment Operators


You are already familiar with the assignment operator (=), which assigns a value to a variable.
int value = 5;
JAVA
This assigned the value 5 to a variable called value of type int.

Java provides a number of assignment operators to make it easier to write code.
Addition and assignment (+=):
int num1 = 4;
int num2 = 8;
num2 += num1; // num2 = num2 + num1;

// num2 is 12 and num1 is 4
JAVA
Subtraction and assignment (-=):
int num1 = 4;
int num2 = 8;
num2 -= num1; // num2 = num2 - num1;

// num2 is 4 and num1 is 4
JAVA
Similarly, Java supports multiplication and assignment (*=), division and assignment (/=), and remainder and assignment (%=).

Assignment Operators

Fill in the missing parts in the following code to print 13.

int x = 25; int y;

y =
- 12;

System.out.println(
);

Chapter: 7

Strings


Strings


String is an object that represents a sequence of characters.
For example, "Hello" is a string of 5 characters.

For example:
String s = "SoloLearn";
JAVA
You are allowed to define an empty string. For example, String str = "";

Strings

Drag and drop from the options below to print "Hello".

String

var;


var = "Hello";


System.out.println(

var

);


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:
String firstName, lastName;
firstName = "David";
lastName = "Williams";

System.out.println("My name is " + firstName +" "+lastName);
JAVA
The char data type represents a single character.

String Concatenation

Which statement in regard to the char data type is true?

"hello" is a char
char is used to store numeric values
'k' is a char

Chapter: 8

Module 1 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(
);

In every Java program...

...there must be at least two variables declared.
...there must be a method called "main".
...all of the variables must be integers.

Drag and drop from the options below to output the name:

String

name;


name = "David";


System

.out.println(

name

);


Code Project

Time Converter


Time Converter


You need a program to convert days to seconds.
The given code takes the amount of days as input. Complete the code to convert it to seconds and output the result.

Sample Input:
12

Sample Output:
1036800

Note: Comment down your answer. 
Explanation: 12 days are 12*24 = 288 hours, which are 288*60 = 17280 minutes, which are 17280*60 = 1036800 seconds.

Comments