Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
42 views

Java Variables

This document provides an introduction to the Java programming language including: - The history and creation of Java by James Gosling at Sun Microsystems in 1995. - How Java code is compiled to bytecode that can run on any machine. - Examples of basic Java programs and code snippets demonstrating core concepts like variables, data types, conditional statements, and printing output. - Explanations of key Java concepts such as classes, methods, primitive data types, and objects.

Uploaded by

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

Java Variables

This document provides an introduction to the Java programming language including: - The history and creation of Java by James Gosling at Sun Microsystems in 1995. - How Java code is compiled to bytecode that can run on any machine. - Examples of basic Java programs and code snippets demonstrating core concepts like variables, data types, conditional statements, and printing output. - Explanations of key Java concepts such as classes, methods, primitive data types, and objects.

Uploaded by

celestiiae
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

“Write Once,

Run Anywhere.”
James Arthur Gosling

Born May 19, 1955 Canada


Residence United States
Nationality Canada
Occupation Computer Scientist
Java programming
Known for
language

On January 27, 2010, Sun was bought by Oracle Corporation


for US $ 7.4 billion, based on an agreement signed on April
20, 2009.
Sun Microsystems, Inc. was subsequently renamed Oracle
America, Inc.
The Java Environment myPr.java
… Java Compiler
If( x>5) y=4 (javac)
else y=-4

Java programmer Java source code myPr.class



1110 0001 1000
1010 0101 0011
Java Virtual Machine …
(java)
Java Byte code

Machine code for PC Machine code for Apple Machine code for Linux
Here is an example Java program. It is about as small as a Java program can be. When
it runs, it writes Hello World! On the computer monitor. The details will be explained
later.

class Hello
{
public static void main ( String[] args )
{
System.out.println("Hello World!");
}
}
System.out.println("Hello World!");

System.out.println("Hello Java!");

System.out.println("Hello World!“ + ” “ + ”Hello Java” );

A statement in a programming language is a command


for the computer to do something. It is like a sentence
of the language.
A statement in Java is followed by a semicolon.
The part "Hello World!" is called a string.

A string is a sequence of characters.

This program writes a string to the monitor and then stops.


What is a variable

Variables are places in the computer's memory where you store


the data for a program.

Each variable is given a unique name which you refer to it with.


All Data

Primitive Data Objects

All data in Java falls into one of two categories: primitive data and objects. There
are only eight primitive data types. However, Java has many types of objects, and
you can invent as many others as you need.
Any data type you invent will be a type of object.

Primitive Data Types

boolea
byte short int long float double char
n
Here is an example of how you declare an integer variable called MyVariable in the
main method:

class Variables
{
public static void main(String[] args)
{
int myVariable;
}
}
If you want to declare more than 1 variable of the same type you must separate the
names with a comma.

public class Variables


{
public static void main(String[] args)
{
int myVariable, myOtherVariable;
}
}
Using variables
A = „equal sign“ is used to store a value in a variable. You put the variable
name on the left side and the value to store in the variable on the right side.
Remember to use quotes when storing String values. You can also store a
value in a variable when it is declared or later in the program.

class Variables
{
public static void main(String[] args)
{
int myInteger;
myInteger = 5;
String myString = "Hello";
}
}
You can print variables on the screen with System.out.println.

class Variables
{
public static void main(String[] args)
{
int myInteger = 5;
String myString = "Hello";
System.out.println(myInteger);
System.out.println(myString);
}
}
You can join things printed with System.out.println with a +.

class Variables
{
public static void main(String[] args)
{
int myInteger = 5;
System.out.println("The value of myInteger is " + myInteger);
}
}
The if else statement syntax looks like this:

if (condition is true)
{
Statement to execute if condition is true;
}
else
{
Statement to execute if condition is false;
}
The condition can contain any of the comparison operators,
sometimes called relational operators:

x == y // x equals y

x != y // x is not equal to y

x>y // x is greater than y

x<y // x is less than y

x >= y // x is greater than or equal to y

x <= y // x is less than or equal to y


what is output?
if (x%2 == 0) {
System.out.println("x is even");
} else {
System.out.println("x is odd");
}
Exercise
What does the following code fragment write to the monitor?

int sum = 14;


if ( sum < 20 )
System.out.print("Under ");
else
{
System.out.print ("Over ");
System.out.println ("the limit.");
}
Exercise
What does the following code fragment write to the monitor?
int sum = 7;
if ( sum > 20 )
{
System.out.print("You win ");
}
else
{
System.out.print("You lose ");
}
System.out.println("the prize.");
Exercise
What does the following code fragment write to the monitor?

int sum = 21;


if ( sum != 20 )
System.out.print("You win ");
else
System.out.print("You lose ");
System.out.println("the prize.");
Exercise 7.What does the following fragment
write?

int count = 0;
System.out.println( count ) ;
Homework #1
count = count + 1;
System.out.println( count ) ;
Exercise 9. Write a Java Program that calculates and
prints the next equation on the screen:

Homework #2

X2, if x ≥ -5
Y=
- X , in other cases

X=5 Y = 25
X = -10 Y = 10
Input data in the program

import java.util.Scanner;

Scanner scan = new Scanner (System.in);


int x = scan.nextInt();
Use
NetBeans IDE
or
https://www.onlinegdb.com/
Homework #3
Homework #4
Homework #5
Homework #6
Homework #7
Homework #8
Looking forward to seeing you again!

You might also like