0% found this document useful (0 votes)
9 views

Java Basics

This document provides an overview of basic Java concepts including statements, expressions, variables, data types, comments, and literals. It defines Java statements as single operations ending in semicolons. Variables are declared with a name and type before being assigned values and can be instance, class, or local. Comments are lines prefaced with // and do not nest. Literals represent simple values like numbers, booleans, characters, and strings denoted with quotes.

Uploaded by

john agai
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Java Basics

This document provides an overview of basic Java concepts including statements, expressions, variables, data types, comments, and literals. It defines Java statements as single operations ending in semicolons. Variables are declared with a name and type before being assigned values and can be instance, class, or local. Comments are lines prefaced with // and do not nest. Literals represent simple values like numbers, booleans, characters, and strings denoted with quotes.

Uploaded by

john agai
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

JAVA BASICS

 Java statements and expressions


 Variables and data types
 Comments
 Literals
 Arithmetic
 Comparisons
 Logical operators

Java statements and expressions


statement forms a single Java operation. The following are simple Java statements:
int i = 1;
import java.awt.Font;
System.out.println(“This motorcycle is a “ + color + “ “ + make);
m.engineState = true;

each statement ends with a semicolon. Forget the semicolon and your Java program won’t
compile.

Variables and Data Types


Variables are locations in memory in which values can be stored. They have a name, a type, and
a value. Before you can use a variable, you have to declare it. After it is declared, you can then
assign values to it.
Statements sometimes return values for example, when you add two numbers together or test to
see whether one value is equal to another. These kind of statements are called expressions.

Types of variables
Java has three types of variables
Instance variables- are used to define attributes or the state for a particular object
Class variables- are similar to instance variables, except their values apply to all that class’s
instances (and to the class itself) rather than having different values for each object.
local variables.- are declared and used inside method definitions, for example, for index counters
in loops, as temporary variables, or to hold values that you need only inside the method
definition itself. They can also be used inside blocks ({}).
Declaring Variables
To use any variable in a Java program, you must first declare it. Variable declarations consist of
a type and a variable name:
int myAge;
String myName;
boolean isTired;

public static void main (String args])


{
int count;
String title;
boolean is Asleep;
...
}
You can string together variable names with the same type:
int x, y, z;
String firstName, LastName;
If there are multiple variables on the same line with only one initializer, You can group
individual variables and initializers on the same line using commas.

Example
Compute area of a circle
1 public class ComputeArea {
2 public static void main(String[] args) {
3 double radius; // Declare radius
4 double area; // Declare area
5
6 // Assign a radius
7 radius = 20; // radius is now 20
8
9 // Compute area
10 area = radius * radius * 3.14159;
11
12 // Display results
13 System.out.println("The area for the circle of radius " +
14 radius + " is " + area);
15 }
16 }

Variables such as radius and area correspond to memory locations. Every variable has a name, a
type, a size, and a value. Line 3 declares that radius can store a double value. The value is not
defined until you assign a value. Line 7 assigns 20 into variable radius. Similarly, line 4 declares
variable area, and line 10 assigns a value into area.

The plus sign (+) has two meanings: one for addition and the other for concatenating
(combining) strings. The plus sign (+) in lines 13–14 is called a string concatenation operator. It
combines two strings into one. If a string is combined with a number, the number is converted
into a string and concatenated with the other string. Therefore, the plus signs (+) in lines 13–14
concatenate strings into a longer string, which is then displayed in the output.

Assigning Values to Variables


Once a variable has been declared, you can assign a value to that variable by using the
assignment operator =:
size = 14;
tooMuchCaffiene = true;
Comments
Comments cannot be nested; that is, you cannot have a comment inside a comment.
Literals
Literals are used to indicate simple values in your Java programs.
Literal is a programming language term, which essentially means that what you type is what you
get.
For example, if you type 4 in a Java program, you automatically get an integer with the value 4.
If you type ‘a’, you get a character with the value a. Literals may seem intuitive most of the time,
but there are some special cases of literals in Java for different kinds of numbers, characters,
strings, and boolean values.
Types of literals
1. Number Literals- these are integer literals
2. Boolean Literals- Consist of keyword true and false
3. Character Literals- are expressed by a single character surrounded by single quotes:
’a’, ’#’, ’3’.
Character codes and their meaning
\n Newline
\t Tab
\b Backspace
\r Carriage return
\f Form feed
\\ Backslash
\’ Single quote
\” Double quote
\ddd Octal
\xdd Hexadecimal
\udddd Unicode character

4. String Literals- A combination of characters is a string. Strings in Java are instances


of the class String. String literals consist of a series of characters inside double
quotes: “Hi, I’m a string literal.”. Strings can contain character constants such as
newline, tab, and Unicode characters:

You might also like