0% found this document useful (0 votes)
4 views33 pages

(Lesson 1) - Output, Variables, Operators

The document provides an introduction to Java, a versatile programming language used for developing various applications across multiple platforms. It outlines the necessary components for setting up a Java development environment, including the Java Development Kit (JDK) and an Integrated Development Environment (IDE) like IntelliJ IDEA. Additionally, it covers basic Java syntax, variable declaration, arithmetic operators, and type casting, along with examples to illustrate these concepts.

Uploaded by

sskx55nchb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views33 pages

(Lesson 1) - Output, Variables, Operators

The document provides an introduction to Java, a versatile programming language used for developing various applications across multiple platforms. It outlines the necessary components for setting up a Java development environment, including the Java Development Kit (JDK) and an Integrated Development Environment (IDE) like IntelliJ IDEA. Additionally, it covers basic Java syntax, variable declaration, arithmetic operators, and type casting, along with examples to illustrate these concepts.

Uploaded by

sskx55nchb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Introduction to Java

Dr. Ali Allam Introduction to Programming (BIS227E)


What is … and how is it used?

❑ Java is a popular programming language, currently owned by


Oracle, and was first released in 1995.
❑ It works on different platforms (Windows, Mac, Linux, Raspberry Pi,
etc.)
❑ It can be used in developing:
❑ Desktop Applications
❑ Web Applications
❑ Mobile Applications (e.g. Android)
❑ Hardware Scripting (e.g. Raspberry Pi)
❑ Database Connection (e.g. Oracle, MySQL, MS Access, etc.)
Java Development Environment

For the first and most common form, you should have the following
components installed, to write and run Java programs:
1. Java Development Kit (JDK)
2. Java Integrated Development Environment (IDE)
(e.g. IntelliJ IDEA, NetBeans, Eclipse, JCreator, etc.)
In our tutorials, we will use one of the available free Java IDEs:
“IntelliJ IDEA” [Community Edition]
Java Development Environment

❑ Many PC’s and Mac’s may have Java already installed. To check if
you have Java installed on your PC, open the Command Line
Prompt (for Windows) or the Terminal (for Mac), type the following
command ( ), and then hit enter:

Java 21.0.1 Java was not found. It


is installed needs to be installed
Java Development Environment

❑ If Java JDK is not installed on your laptop, download the latest


version from the following link:
❑ Windows: https://download.oracle.com/java/21/latest/jdk-21_windows-
x64_bin.exe
❑ Mac: https://download.oracle.com/java/21/latest/jdk-21_macos-x64_bin.dmg
❑ As for IntelliJ IDEA [Community Edition], it can be installed from
the following link:
❑ Windows: https://www.jetbrains.com/idea/download/?section=windows
❑ Mac: https://www.jetbrains.com/idea/download/?section=mac
The interface of IntelliJ IDEA

1. Open IntelliJ IDEA


2. To create your first project,
click on + New Project
Type the name of your new project
This is the location where you can find
your project(s)

You can uncheck this if you wish. It’s just an


auto-generated sample code

Click on the Create button


The interface of IntelliJ IDEA

Right-click on the the src folder to create a new Java file:


The interface of IntelliJ IDEA

Type a name for your Java file (e.g. MyProgram1), and then hit enter:
Java Quick Start: Basic Syntax

Java Basic Syntax


Java is a case-sensitive programming language. For instance, a variable called
num1 is not the same as Num1. Java will consider them two different variables.
Any statement must in Java must be terminated using a semi-colon → ;
The semi-colon indicates the end of the statement.
Java uses braces/curly-brackets { } to indicate blocks of code for a function, a
loop, an if statement, or a class.
Java files/programs have the extension “.java”
In Java, every application begins with a class name, and that class must match
the filename.
Java Quick Start: Hello World Program

Every program that runs in Java must be


placed inside a class. In our example, we public class HelloProgram
named the class HelloProgram. {
The main function is the entry point at any public static void main(String[ ] args)
{
java file, from which all your lines of code will
System.out.println(“Welcome to Java");
be executed. }
The curly brackets { } mark the start and end }
of a block of code. Also, any statement in
your code must end with a semi-colon.
Output Statements
Output Statements

❑ To display an output in Java, one of the two following ways is used:


❑ System.out.print(……….)
❑ System.out.println(……….) → makes a new line break after the output
❑ There are more other ways to display an output, that will be covered later.
❑ Example: System.out.println("My name is Ali");
❑ What kinds of output can be printed?
1. Plaintext only
2. Variables only
3. Both together, Plaintext and Variables
1. Print plaintext

Text (strings) must be enclosed in “double quotes”:


Prog1.java Output Console

public class Prog1


My name is Ali. I like programming.
{
I am interested in Java.
public static void main(String[ ] args)
{
System.out.print("My name is Ali. ");
System.out.println("I like programming.");
System.out.print("I am interested in Java.");
}
}
1. Print plaintext

❑ The special character \n can be public class Prog2


{
used to make a line break public static void main(String[ ] args)
within the print command. {
System.out.print("Hello,\n\n");
❑ The special character \t can be System.out.println("My\tname is Ali.");
used to make a tab space within System.out.print("I am\tinterested in Java.");
}
the print command. }
❑ These special characters may
be useful in formatting or Hello,

organizing the output. My name is Ali.


I am interested in Java.
2. Print variables

You can also print the value of a given variable, without using the quotes, just
the name of the variable:
public class Prog3
{
Output Console
public static void main(String[ ] args)
{
String name = "Ali"; Ali
int age = 24; 24
double gpa = 3.75; 3.75
System.out.println(name);
System.out.println(age);
System.out.println(gpa);
}
}
3. Print (plaintext + variables)
 Variables are concatenated together with plaintext, using the plus sign ( + ),
such as follows:
public class Prog4
{
public static void main(String[ ] args) Output Console
{
String name = "Ali"; My name is Ali
int age = 24; I am 24 years old
double gpa = 3.75; My GPA is 3.75
System.out.println("My name is " +name);
System.out.println("I am "+age+" years old ");
System.out.println("My GPA is " +gpa);
}
}
Comment Statements
Comment Statements

 A comment is a message written within the source program and is


used to document or explain the source code.
 Makes the program more explained and eye-catching.
 Non-executable statement which is always ignored by the java
compiler.
 Single-line comment is written after using double-slash //
// comment
 Multiple-line comments are enclosed in between /* */
/* comment1
comment2 */
Comment Statements

public class Prog5


{
public static void main(String[ ] args)
{
// This is a single-line comment
int age = 24; // the value of age is 24
/*This is a multiple-line comment
The program will print the age in a message
*/
System.out.println("I am "+age+" years old ");
}
}
Java Variables
Variables

❑ As you know, variables are used to store data values within the
program, such as input data or output data (e.g. name, radius, age,
sum, average, area, maximum, etc.)
❑ In Java, the name of a variable can contain characters (letters) and
numbers, but it must start with a character. It cannot contain spaces.
Only the underscore and dollar sign, are allowed. For example:
Invalid variable names Valid variable names
first-name first_name
first name firstname
1st_name Name$1
Variables

❑ In Java, the type of a variable must be explicitly declared. The value


assigned to the declared variable must be consistent with the type of
that variable.
❑ int a = -3; → integer variable
❑ float b = 3.49f; → float variable. Letter f must be written after the decimal value
❑ double c = 6.0; → double variable
❑ int d = 5+1; → integer variable
❑ String e = “Ali”; → String variable. It stores text values, using double quotes
❑ char f = ‘x’; → character variable. It stores one character, using single quotes
❑ boolean g = true; → boolean variable. It stores either true or false
❑ and even more other types … such as byte, short, and long.
Notes on variables declaration

❑ We can declare a variable and assign its value together in the same
statement:
double gpa=3.5; // declare a double variable, and assign a value of 3.5
❑ And we can also split the declaration and the assigned value into two
separate statements:
double gpa; // declare a double variable called gpa
gpa = 3.5; // assign a value of 3.5 to gpa. Do NOT re-declare the variable
❑ We cannot declare a variable twice. In other words, we cannot have two
variables declared with the same name, even if they have different types:
float x=3.0f; // declare a float variable called x of value 3.0
int x=2; // Syntax Error. We already have a variable called x
Example of using variables

public class Prog6


{
public static void main(String[ ] args)
Output Console
{
String name = "Ali"; // String variable of value Ali
My name is Ali
int age = 24; // integer variable of value 24
I am 24 years old
float gpa = 3.75f; // float variable of value 3.75
My GPA is 3.75
System.out.println("My name is " +name);
System.out.println("I am "+age+" years old ");
System.out.println("My GPA is " +gpa);
}
}
Arithmetic Operators
Arithmetic Operators

Operator Name Explanation Example


x+y Addition / Add x to y, if they are numbers 5+2→7
Concatenation Concatenate x and y, if they are Strings "Ali" + "Allam" →
"AliAllam"
x–y Subtraction Subtract (y) from (x) 7–4→3
x*y Multiplication Multiply (x) by (y) 6 * 3 → 18
x/y Division Result of dividing (x) by (y) 9 / 2 → 4.5
x%y Modulus Remainder of dividing (x) by (y) 9%2→1
Notes on arithmetic operators

❑ The precedence (priority) of evaluating the operators are as follows:


1. Parenthesis/Brackets: ( )
2. Multiplication, division, modulus: * / %
3. Addition and subtraction: + -
Important Notes:
▪ If two operators have the same precedence, then the expression is
evaluated from left-to-right.
▪ The evaluated expression is casted / converted to the type of the
variable to which it is assigned.
▪ Truncation occurs when integer division is performed.
public class Prog7
{
public static void main(String[ ] args) Example
{
int a = 10;
int b = 3;
int sum = a+b;
int diff = a-b; Output Console
int prod = a*b;
double wrong_avg = (a+b)/2; // truncated due to integer division The sum is 13
double correct_avg = (a+b)/2.0; The difference is 7
int rem = a%b; The product is 30
System.out.println("The sum is " +sum); The wrong average is 6.0
System.out.println("The difference is " +diff); The correct average is 6.5
System.out.println("The product is " +prod); The remainder is 1
System.out.println("The wrong average is " +wrong_avg);
System.out.println("The correct average is " +correct_avg);
System.out.println("The remainder is " +rem);
}
}
Type Casting
Type Casting: Widening

 It is useful in many cases to cast/convert a variable into another different


type. Casting can be done in different two ways: Widening and Narrowing.
❑ Widening: converting a smaller type to a larger type size.
byte → short → char → int → long → float → double
❑ Widening is done automatically in Java when a small-size value is
assigned to a large-size variable. For example:
double x = 5*6; // int value is assigned to a double variable
System.out.println(x); // The program will print 30.0
Type Casting: Narrowing

❑ Narrowing: converting a larger type to a smaller type size.


byte  short  char  int  long  float  double
❑ Narrowing is done manually by placing the type in parentheses in front of
the value. For example:
double x=5.7; //declare a double variable of value 5.7
int y = (int)x; //the value of x is narrowed to int and assigned to y
System.out.println(x); //the program will print 5.7
System.out.println(y) //the program will print 5
Type Casting Example

public class Prog8


{
public static void main(String[ ] args)
{
int a = 7;
int b =2; Output
float x = (a*b); // widening: (a*b) casts from an integer value to a float variable
System.out.println (x); 14.0
float y = a/b; //truncation occurs, and then the value is widened to a float variable 3.0
System.out.println(y); 3
int z = (int)(1.0*a/b); //the calculated expression casts from a double value to an integer
System.out.println(z);

}
}

You might also like