Download Programming languages

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Introduction to Programming
G50PRO
University of Nottingham
Unit 4 – Introduction to Java
Paul Tennent
http://paultennent.wordpress.com/G50PRO.html
[email protected]
Room C7
Overview





How Java works?
Break up Of The HelloWorld program
Java Data types
Using Variables
Expressions, Statements, and Blocks
Programming languages

An interpreted language is a programming
language whose programs are translated to
machine code at the time of execution
through using an interpreter program

A compiled language is a programming
language which need the use of compilers to
generate executable machine code in order
to run the program
Compiled Languages
executable machine code
import java.lang.*
import
java.lang.*
--------------------import
java.lang.*
---------------------------------------import
java.lang.*
--------------------------------------------------------------------------------------------Source
Code
---------------------- ------------Print------Hello
World
-------------------------------- ------ ----------------------------------------------
Compiler
0010010100110
100101010101
101010101
10101010
Run
High Level Language code
Hello World!
Interpreted Languages
import java.lang.*
import
java.lang.*
--------------------import
java.lang.*
---------------------------------------import
java.lang.*
--------------------------------------------------------------------------------------------Source
Code
---------------------- ------------Print------Hello
World
-------------------------------- ------ ----------------------------------------------
High Level Language code
Interpreter
Run
Hello World!
Java language is both
compiled and interpreted

In-stead of translating Java programs into machine
language, the Java compiler generates Java byte
code

Byte code is easy (and fast) to interpret, almost like
machine language, yet it is also portable, thus it is
possible to compile a Java program on one machine
and run it on any other machine

Java virtual machine is needed to run any java
program
Java virtual machine

A Java virtual machine (JVM) interprets compiled
Java byte code for a computer's CPU so that it can
perform a Java program's instructions

Defines an abstract machine or processor.

Once a Java virtual machine has been implemented
for a given platform, any Java program can run on
that platform
Running Java Programs
javac
java
HelloWorld Program
/**
* The HelloWorld class implements an application that
* simply prints "Hello World!" to standard output.
*/
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}// main end
} // class end
Break down of HelloWorld
/**
* The HelloWorld class implements an application that
* simply prints "Hello World!" to standard output.
*/
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}// main end
} // class end
every application must
The keyword class begins
contain a main method
Comments
the
class definition
are ignored
for aby
(The starting point of the
the compiler
class
followedbut
byare
its name.
useful
program)
to other programmers.
The modifiers public and
The code
Java programming
for each class
static can be written in
languagebetween
appears
supportsthe
three
either order yet the
kinds of comments
opening
and closing curly
convention is to use public
braces
static
Hello World
Anything in these brackets is information given to
This means that this process has no return
the
method
tothathelp
it toperform
its job
is simply
the
of the
method
For nowThis
– just assume
this
hasname
be there!
This simply means that other files can use this method if they wish
public static
void main (String[] args) {
System.out.println ("Hello World!")
;
}// main end
Here is an example
oflike
using
method.
case, we’re
A semi-colon
is used
a afull
stop. InItitthis
indicates
the using
endit’sof
This is us giving
the method
information
needs to complete
the method that writes text on to the screen.
job. In this
we’re
telling it what to write!
a case,
single
instruction
Notes


Java is case-sensitive
public class HelloWorld is the declaration of a new class called HelloWorld.

main is the entry point for the program, that is the point at which execution
starts.

The body of the class and main method is contained within the { and }
symbols.

Every statement which is an instruction to the computer must be ended with
a semi-colon.

main() and { and } are part of the layout of the program not instructions.

White space layout (tabs, newlines, spaces etc) is not enforced but should
be used sensibly to make the code more readable.
Typed Languages
BankBalance
HolderName


700
John Smith
Primitive Data Types

all variables must first be declared before they can be used

byte: An 8-bit signed integer ( -128 to 127 )

short: A 16-bit signed integer. (-32,768 to 32,767 )

int: A 32-bit signed integer. It has a minimum value of 2,147,483,648 and a maximum value of 2,147,483,647

long: A 64-bit signed integer. It has a minimum value of 9,223,372,036,854,775,808 and a maximum value of
9,223,372,036,854,775,807
Primitive Data Types

float: A single-precision 32-bit floating point.

double: a double-precision 64-bit floating point.

boolean: The boolean data type has only two
possible values: true and false.

char: The char data type is a single 16-bit Unicode
character. It has a minimum value of '\u0000' (or 0)
and a maximum value of '\uffff' (or 65,535 inclusive).
Reference Data Types - Strings

Typically, a reference is the memory address
at which the object or array is stored.

String str =“cat”;

Can not use == for comparison use ‘equals’ method instead
String x = "Hello";
String y = "World";
String z = "HelloWorld";
String a = x + y;
System.out.println(a == z);
System.out.println(a.equals(z));
Creating a Variable in Java

To create a variable in java we simply specify the
type and the name of the variable.

The following defines a variable called “num” that
holds integers.


To initialize the variable:


int num;
num = 0;
Can be done on one step:

int num =0;
Creating a Variable in Java

We have to create a variable before we can use it

Creating variables simply warns the computer that it
needs to allocate some of the RAM to storing your
data.

The more variables you create, the more RAM your
program will use when it is run.
Simple Program
class Demo {
public static void main (String[] args) {
int result = 1 + 2; // result is now 3
result = result - 1; // result is now 2
result = result * 2; // result is now 4
result = result / 2; // result is now 2
result = result + 8; // result is now 10
result = result % 7; // result is now 3
System.out.println(result);
} // main end
} // class end
Expressions, Statements, and
Blocks

Operators may be used in building expressions, which compute values.



Expressions are the core components of statements (ends with ; )




5+(7*2)
5>9
int x= 5+7;
x++;
System.out.println("Hello World!");
Statements may be grouped into blocks
defined by starting { and ending with }

{
int x;
x =0;
System.out.println(“x value is” + x);
}
Summary





How Java works?
Break up Of The HelloWorld program
Java Data types
Using Variables
Expressions, Statements, and Blocks
Recommended Reading

http://java.sun.com/docs/books/tutorial/java/n
utsandbolts/index.html