0% found this document useful (0 votes)
2 views26 pages

Lecture 2 - Basic Concepts in Java

The document provides an overview of basic Java concepts including comments, variables, operators, and user input. It explains the types of comments supported in Java, the various data types for variables, and the arithmetic and assignment operators. Additionally, it introduces the Scanner class for obtaining user input in Java programs.
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)
2 views26 pages

Lecture 2 - Basic Concepts in Java

The document provides an overview of basic Java concepts including comments, variables, operators, and user input. It explains the types of comments supported in Java, the various data types for variables, and the arithmetic and assignment operators. Additionally, it introduces the Scanner class for obtaining user input in Java programs.
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/ 26

Basic Concepts in

Java
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.
• A single-line comment starts with two
forward slashes and continues until it
reaches the end of the line.
Example
Multi-Line Comments
 Java also supports comments that span multiple
lines.
Example
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.
Example
Variables
 Variables store data for processing
 A variable is given a name (or identifier),
such as area, age, height, and etc. The
name uniquely identifies each variable,
assigning a value to the variable and
retrieving the value stored.
There are many data types for variables,
some examples are

• int – used to declare an integer (whole numbers) such


as 112 and -763.
• double – for floating-point or real numbers with
optional decimal points and fractional parts in fixed or
scientific notations, such as 3.1415 and -55.61.
• String – for sequence of characters such as “Hello” or
“Good Morning!”. Text strings are enclosed within
straight double quotes ("").
• char – for variables that hold a single alphanumeric
character, such as a, Z, or 1.
• boolean – only accepts two (2) possible values: true
and false.
Example
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.
• Example
• int x = 9 + 5; // 14
Java Arithmetic Operators
 Addition – The plus sign (+) operator adds together
two (2) values, such as two (2) constants, a constant
and a variable, or a variable and a variable.
 Subtraction – The dash (–) operator subtracts one
value from another.
 Multiplication – The asterisk (*) operator
multiplies two (2) values.
 Division – The forward slash (/) operator
divides one value by another.
 Modulo – The modulo (or remainder) math
operation performs an integer (whole
number) division of one value by another
and returns the remainder of that division.
The operator for the modulo operation is
the percentage (%) character.
Example
Assignment Operators
 The assignment operator (=), which assigns
a value to a variable.
 This assigned the value 5 to a variable
called value of type int.
Example:
This assigned the value “MyMaster” to a
variable called value of type String.
String Concatenation
 The + (plus sign) operator between string
values adds them together to make a new
string. This process is called concatenation.
 The resulting string is the first string, put
together with the second string.
Example:
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 Scannerclass to use
the Scanner object, as seen below:
• import java.util.Scanner;
• In order to use the Scanner class, create an
instance of the class by using the following
syntax:
Scanner myVar = new Scanner(System.in);
• You can now read in different kinds of input
data that the user enters.
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());
}
}
END

You might also like