Basic Java Syntax The Java language will be described by working through its features: variable types and expressions selection and iteration classes exceptions.

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

Programming Languages and Paradigms
Programming Languages and Paradigms The C Programming Language.
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Java Syntax Primitive data types Operators Control statements.
Pemrograman Dasar - Data Types1 THINGS & STUFF Identifier, Variable, Constant, Data type, operator, expression, assignment, javadoc.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
JavaServer Pages Syntax Harry Richard Erwin, PhD CSE301/CIT304.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
DAT602 Database Application Development Lecture 5 JAVA Review.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Structure import java_packages; class JavaClass { member variables declarations; void otherMethod( ) { } public static void main(String[]
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Vladimir Misic: Java1 Basic Java Syntax The java language will be described by working through its features: –Variable types and expressions.
Introduction to Java Java Translation Program Structure
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
By Mr. Muhammad Pervez Akhtar
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Object Oriented Programming Lecture 2: BallWorld.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
More on Arrays Review of Arrays of ints, doubles, chars
Information and Computer Sciences University of Hawaii, Manoa
Basic Introduction to C#
Crash course in the Java Programming Language
BASIC ELEMENTS OF A COMPUTER PROGRAM
Lecture 5: Some more Java!
Java Primer 1: Types, Classes and Operators
Programming Languages and Paradigms
CS180 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Multiple variables can be created in one declaration
Programming Paradigms
C Basics.
Java Programming: From Problem Analysis to Program Design, 4e
Primitive Types Vs. Reference Types, Strings, Enumerations
An Introduction to Java – Part I
Advanced Programming Behnam Hatami Fall 2017.
Starting JavaProgramming
Java - Data Types, Variables, and Arrays
Introduction to Java Programming
An Introduction to Java – Part I, language basics
An overview of Java, Data types and variables
Java Tokens & Data types
PHP.
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
elementary programming
C Programming Getting started Variables Basic C operators Conditionals
Java Basics Data Types in Java.
PHP an introduction.
Chap 2. Identifiers, Keywords, and Types
Arrays Arrays are represented by objects but there is no class that array objects are instances of. Variables of array type are declared using bracket.
Presentation transcript:

Basic Java Syntax The Java language will be described by working through its features: variable types and expressions selection and iteration classes exceptions Small sample programs will be provided to illustrate how each feature is used. 9/18/2018 Basic Java Syntax

Comments Comments come in three forms: // single line comments /* multi line comment */ /** a * Javadoc * comment 9/18/2018 Basic Java Syntax

Javadoc A tool that comes with the JDK that produces HTML-based documentation from Java Source code. Within a Javadoc comment, various tags can appear which allow additional information to be processed. Each tag is marked by an @ symbol and should start on a new line. 9/18/2018 Basic Java Syntax

Javadoc Tags 9/18/2018 Basic Java Syntax

Example 9/18/2018 Basic Java Syntax /** * A class that manages a circle given the radius * @see java.lang.Math * @version 1.0 * @author Paul Tymann */ public class Circle { private double radius; * Constructor for a circle. * * @param radius radius of the circle being created. Must be * positive and greater than 0. public Circle( double radius ) { this.radius = radius; } 9/18/2018 Basic Java Syntax

The Result The result is a set of HTML pages. The documentation that is produced is meant to be part of the overall documentation that comes with the JDK. So you need to copy some files to get things to work. 9/18/2018 Basic Java Syntax

Primitive Types Java has two categories of types: primitive types and reference types. The primitive types represent the basic, built-in types that are part of the Java language. Two basic categories: Boolean - boolean Numeric Intergal - byte, short, int, long, char Floating point - float, double 9/18/2018 Basic Java Syntax

Primitive Types 9/18/2018 Basic Java Syntax

Unicode An International Standard that defines the representation of characters from a wide range of alphabets. Unicode stores characters as 16-bit values providing 65,536 different characters. ASCII happens to be the first 127 characters in the Unicode standard. Java uses Unicode as opposed to ASCII. 9/18/2018 Basic Java Syntax

Unicode Escapes Unicode escapes allow any character to be represented regardless of the editor being used A Unicode escape stands for a character and is represented using the \u escape sequence followed by the hexadecimal digits of the character code Examples: \u0343, \u2f4, \uabcd 9/18/2018 Basic Java Syntax

Literals 9/18/2018 Basic Java Syntax

Automatic Type Conversion Java provides a variety of automatic type conversions. The following conversions are supported: Widening primitive conversions byte to short, int, long, float, or double short to int, long, float, or double int to long, float, or double long to float or double float to double 9/18/2018 Basic Java Syntax

Automatic Type Conversions Widening Reference Conversions these allow a reference of a subclass type to be treated as a reference of a superclass type. String conversion when the ‘+’ (string concatenation) operator has one argument of type of type String the other argument can be converted from any other type to type String Conversions like these are performed during assignment and parameter passing. 9/18/2018 Basic Java Syntax

Identifiers Variables, methods, classes and interfaces all need to be named. Identifiers start with an alphabetic character can contain letters, digits, or “_” are unlimited in length Examples Answer, total, last_total, relativePosition, gridElement Person, Place, Stack, Queue 9/18/2018 Basic Java Syntax

Declaring Variables The basic syntax for declaring variables is: It is possible to declare two or more variables of the same type in a single declaration statement. typename identifier; or typename identifier = expression; 9/18/2018 Basic Java Syntax

Categories of Variables There are two categories of variables: Variables of primitive type which directly contain a representation of a value of a primitive type. Variables of a reference type which hold a reference to an object conforming to the named type or the value null (which is the null reference). All variables must be declared and declared before being used. 9/18/2018 Basic Java Syntax

Default Initialization 9/18/2018 Basic Java Syntax

Example 9/18/2018 Basic Java Syntax public class var1 { public static void main( String args[] ) { int i=1; String s = “hello”; int j; // j cannot be used yet since it does not have a value j = 4; System.out.println( j ); float a = 1.0f, b = 2.0f, c = 3.0f; double pi = 3.14; System.out.println( pi ); System.out.println( s ); } 9/18/2018 Basic Java Syntax

Operators 9/18/2018 Basic Java Syntax

And and Or The &&, ||, &, and | operators operate differently from C && and || can only be applied to boolean values What happens with & and | depends on the types of the arguments: if used with integral values the operations are bitwise if used with boolean values the operations are boolean and are NOT short-circuited 9/18/2018 Basic Java Syntax

Statement The statement is the main building block from which code sequences are constructed. Statements are executed in the order listed and are always terminated by a semicolon. expr; or { expr1; expr2; … exprn; } 9/18/2018 Basic Java Syntax

The if Statement Syntax: Note you can layout code in any way you want. if ( booleanExpression ) statement or if ( booleanExpression ) statement else 9/18/2018 Basic Java Syntax

The switch statement Syntax: As in C, break statements are needed to jump out of a switch statement. The default case is optional. switch ( expression ) { case char/byte/short/int constant : statementSequence … default: statementSequence 9/18/2018 Basic Java Syntax

Example int z; switch ( i ) { case 1: z = 1; break; case 2: z = 2; default: z = 0; } 9/18/2018 Basic Java Syntax

The while Loop Syntax: while ( booleanExpression ) statement 9/18/2018 Basic Java Syntax

The do Loop Syntax: do statement while ( booleanExpression ); 9/18/2018 Basic Java Syntax

The for Loop Syntax: Each of the expressions is optional, the semicolons are not. A for loop is basically a while loop with initialization and updating thrown in. for ( initExpr; booleanExpr; updateExpr ) statement 9/18/2018 Basic Java Syntax

Transfer Statements The break statement can occur anywhere within a switch, for, while or do statement and causes execution to jump to the next statement. The continue statement can occur anywhere within a for, while or do statement and causes execution to jump to the end of the loop body. The return statement causes the execution of the current method, with control returning to the caller. 9/18/2018 Basic Java Syntax

Objects An object is a structure that represents a state and knows methods to manipulate it. The structure components are called instance variables. Given a class, one normally creates objects. Objects are created dynamically with operator new which in turn calls a constructor method to initialize the instance variables. Methods mostly access the instance variables of the receiver. 9/18/2018 Basic Java Syntax

Java Classes The Java system comes with an extensive set of classes from which you may create objects. Lets start with a familiar class String. To find out what you can do to Java strings you need to refer to the documentation that comes with the JDK 9/18/2018 Basic Java Syntax

Name.java // A simple program that exercises some basic methods // in the String class. public class Name { public static void main( String args[] ) { String name; int midLoc; name = "Paul"; name = name.concat( " Tymann" ); midLoc = name.indexOf( " " ); name = name.substring( 0, midLoc ) + " Thomas" + name.substring( midLoc ); System.out.println( name ); for (int i=0; i<name.length() && name.charAt(i) != ' '; i++ ) System.out.println( name.charAt(i) ); } 9/18/2018 Basic Java Syntax

Reverse.java // This program reverse a given string public class Reverse { public static void main( String args[] ) { String orig = "Hello World"; String reverse = ""; for (int i=0; i<orig.length(); i++) reverse = orig.charAt( i ) + reverse; System.out.println( reverse ); } 9/18/2018 Basic Java Syntax

StringBuffer The String class provides string objects that cannot be changed. The StringBuffer class provides mutable string objects. 9/18/2018 Basic Java Syntax

Reverse2 // Another way to reverse a string public class Reverse2 { public static void main( String args[] ) { StringBuffer rev = new StringBuffer ( “Hello World” ); char tmp; for (int i=0,j=rev.length()-1; i<j; i++,j-- ) { tmp = rev.charAt( i ); rev.setCharAt(i, rev.charAt(j) ); rev.setCharAt(j, tmp ); } System.out.println( rev ); 9/18/2018 Basic Java Syntax

Palin // This program checks a given string to see if it is a palindrome public class Palin { public static void main( String args[] ) { String orig = "mom”, reverse = ""; // Reverse it for (int i=0; i<orig.length(); i++) reverse = orig.charAt( i ) + reverse; // Now check it ( note that orig == reverse does not work ) if (orig.equalsIgnoreCase(reverse)) System.out.println( "Palindrome" ); else System.out.println( "Not a palindrome" ); } 9/18/2018 Basic Java Syntax

Arrays Arrays are represented by objects but there is no class that array objects are instances of. Variables of array type are declared using bracket ([]) notation: typename[] varname; or typename[] varname = arrayInitExpr; typename varname[]; typename varname[] = arrayInitExpr; 9/18/2018 Basic Java Syntax

Arrays Multi-dimension arrays can be declared by repeating pairs of brackets up to the required dimension. The length instance variable holds the size or length of the array: String[] words = new String[100]; System.out.println( words.length ); int [][] twoD = new int[10][20]; System.out.println( twoD.length ); // gives 10 System.out.println( twoD[0].length ); // gives 20 9/18/2018 Basic Java Syntax

Array Initialization It is possible to directly initialize the values of the array elements using an initializer list: int[] n = { 1, 2, 3, 4, 5 }; int [][] m = { {1, 2, 3, 4}, {4, 5, 6, 7}}; int [][] w = { {1, 2, 3}, { 4, 5}}; 9/18/2018 Basic Java Syntax

CmdLineEcho // Echo the contents of the command line public class CmdLineEcho { public static void main( String args[] ) { for (int i=0; i<args.length; i++) System.out.println( args[i] ); } 9/18/2018 Basic Java Syntax

Vectors The Vector class implements a growable array of objects Like an array, its components can be accessed by integer indexes. There is one important difference between Vectors and arrays: arrays can hold any type Vectors can only hold instances of Object 9/18/2018 Basic Java Syntax

InsertSort import java.util.*; public class InsertSort { public static void main( String args[] ) { Vector data = new Vector(); for (int i=0; i<10; i++) { int newVal, j; newVal = (int)(Math.random() * 100); for( j=0; j<data.size(); j++) if ( newVal < ((Integer)data.elementAt(j)).intValue() ) break; data.insertElementAt( new Integer( newVal), j ); } for (int i=0; i<10; i++) System.out.println( data.elementAt( i ) ); }} 9/18/2018 Basic Java Syntax