Download Introduction to Java Programming

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

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

Document related concepts
no text concepts found
Transcript
College Preparatory Program • Saudi Aramco
Introduction to Java Programming
Introduction to Java Programming
VARIABLE
A variable is a memory location whose value can vary when the program is running. All variables must
have a name and type. A variable’s type indicates what kind of data is stored in the memory location.
See the following website for information on:
1. Naming variables
2. The range of floating point and integer variables
3. Incrementing and decrementing
4. Casting variables from one type to another
http://richardbowles.tripod.com/java/guide/varprobs.htm
DATA TYPES
Every piece of data in Java has a type associated with it. There are two major categories of data types in
the Java language: primitive types and reference types.
PRIMITIVE DATA TYPES
Primitive type can store only single value. Java has eight primitive data types: two floating
(number with decimal point) points, four integer s (whole numbers), a character type and a boolean.
See http://www.cafeaulait.org/course/week2/02.html for data types size.
REFERENCE DATA TYPES
In Java a reference data type is a variable that can contain the reference or an address of
dynamically created object. Examples include arrays, classes and interfaces that are defined
by the user. http://docstore.mik.ua/orelly/java/langref/ch03_02.htm
OPERATOR PRECEDENCE
Java operator precedence is how Java decides which operator to evaluate first.
Visit http://www.cis.upenn.edu/~palsetia/java/precedenceTable.html for a chart showing precedent
from highest to lowest precedence.
WHAT IS A METHOD?
A method is a group of instructions that can do a specific job and is given a name. A method can be
called at any point in a program simply by quoting that name. Java methods are similar to functions and
procedures in other programming languages. To learn more about methods, method return types and
passing arguments to methods visit: http://www.video-animation.com/java_011.shtml ,
http://mathbits.com/mathbits/java/methods/Lesson1.htm and
http://www.horsesnw.com/articles/JavaMethod.htm .
College Preparatory Program • Saudi Aramco
Introduction to Java Programming
PARTS OF A SIMPLE JAVA PROGRAM
Every Java program must have one main() method. Visit the following URL:
http://mathbits.com/mathbits/java/Introduction/ProgramParts.htm
to understand the fundamental parts of a standard Java program.
SWAPPING OF TWO NUMBERS
temp = num1;
num1 = num2;
num2 = temp;
BASE NUMBER CONVERSION
Decimal numbers use base 10. Binary numbers use 10.Hexadecimal numbers use base 16.
For interconversion between different number types see
http://www.eecs.wsu.edu/~ee314/handouts/numsys.pdf.
THE ‘FOR’ STATEMENT EXPLAINED
for(<initialization>; <boolean expression>; <update>){
<statement or statements>
}
Example: Adding numbers from 1 to 50
for(int i=0; i <= 50; i++){
sum += i;
{
For more information and comparison of the for statement with the while statement visit:
http://leepoint.net/notes-java/flow/loops/for.html
INPUT STRING
Scanner npt = new Scanner(System.in);
String lastName;
System.out.print(“Plz enter your last name: “);
lastName = npt.next();
CLASSES AND OBJECTS
A class can be defined as a template or blueprint, which describes the data (instance variables)
enclosed within, and behavior (methods) common to all objects of particular kinds.
An object is an implementation of class. It is a software bundle of variables and methods.
Objects are also called instances of a class. See http://java.sun.com/docs/books/tutorial/java/concepts/
http://www.youtube.com/watch?v=R0hJQUaF4uk&feature=related
http://www.ensta.fr/~diam/java/online/JavaBasics/oop/oop-10-intro.html
ftp://ftp.prenhall.com/pub/esm/sample_chapters/engineering_computer_science/deitel/SmallJavaHTP
6e/pdf/sjhtp6_03.pdf
College Preparatory Program • Saudi Aramco
Introduction to Java Programming
ARRAYS
An array is simply a sequence of either objects or primitives all the same type and packaged
together under one identifier name. Read the following to learn how arrays work:
http://www.javaclass.info/classes/java-array/array-examples-demonstration-and-code-snippets.php
http://www.learn-java-tutorial.com/Java-Arrays.cfm
http://www.youtube.com/watch?v=0MIyqDtDs_M
VARIABLE SCOPE
A variable's scope is the block of code within which the variable is accessible.
You establish the scope of a variable when you declare it. For example visit:
http://www.java2s.com/Tutorial/Java/0020__Language/VariableScope.htm
OBJECT CREATION
We create an object by invoking the new operator. The syntax of new is:
<object name> = new <class name> (<arguments>)
Example
Student smith = new Student(); // No arguments are used here.
Explanation
An object of type Student is created and the identifier smith is set to refer to it.
IMPORT STATEMENT
An import statement simply provides an address where compiler can look for class definitions.
The syntax of import is: import <package name> . <class name>
Example:
import javax.swing.JOptionPane;
javax.swing is the package, which is a group of related classes, . is the dot operator,
JOptionPane is the class that our program needs. For more information visit:
http://leepoint.net/notes-java/language/10basics/import.html