Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Written by: Dr. JJ Shepherd
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Constants and Data Types Constants Data Types Reading for this class: L&L,
Java Syntax Part I Comments Identifiers Primitive Data Types Assignment.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
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.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
1 Course Lectures Available on line:
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
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 Java Programming Language
CSC204 – Programming I Lecture 4 August 28, 2002.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
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.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Chapter 2: Java Fundamentals
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
 Character set is a set of valid characters that a language can recognise.  A character represents any letter, digit or any other sign  Java uses the.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
Java Programming Java Basics. Data Types Java has two main categories of data types: –Primitive data types Built in data types Many very similar to C++
Copyright Curt Hill Variables What are they? Why do we need them?
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Written by: Dr. JJ Shepherd
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
Casting, Wrapper Classes, Static Methods, JOptionPane Class.
Data Types References:  Data Type:  In computer science and computer programming, a data type or simply type is a.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Object Oriented Programming Lecture 2: BallWorld.
Topics introduced today (these topics would be covered in more detail in later classes) – Primitive Data types Variables Methods “for” loop “if-else” statement.
Information and Computer Sciences University of Hawaii, Manoa
Chapter 7 User-Defined Methods.
Crash course in the Java Programming Language
Outline Creating Objects The String Class Packages Formatting Output
Yanal Alahmad Java Workshop Yanal Alahmad
Lecture 2: Data Types, Variables, Operators, and Expressions
2.5 Another Java Application: Adding Integers
University of Central Florida COP 3330 Object Oriented Programming
null, true, and false are also reserved.
Introduction to Java Programming
An Introduction to Java – Part I, language basics
An overview of Java, Data types and variables
Java Classes and Objects 3rd Lecture
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Java Basics Data Types in Java.
Names of variables, functions, classes
Chap 2. Identifiers, Keywords, and Types
Agenda Types and identifiers Practice Assignment Keywords in Java
Presentation transcript:

Introduction to Java Lecture Notes 3

Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical to c/c++ int n; int m = 5; double x;

Variables with one modification, instead of const Java has final: final int N = 10; meaning that the value of N cannot be changed anymore. Java is a strongly typed language, the value on the rhs (right hand side) must be compatible with the type on the lhs. int x = 2.3; is illegal, however double x = 2; is ok.

Types Java has TWO general types: primitive types and reference types. There are, correspondingly, two kinds of data values that can be stored in variables, or passed as arguments, or returned by methods, and operated on: primitive values (or data) and reference values. Primitive data - numbers and characters. In the declaration int n; char ch = 'a'; int and char are types. Reference data - objects which are defined by a class, for example String str = "15-111";

Primitive Types A primitive type is predefined by the Java programming language and named by its reserved keyword. There are the char type; 4 integer types: byte (1 byte), short (2 bytes), int (4 bytes), long (8 bytes), and char (2 bytes); 2 floating types: float (4 bytes), double (8 bytes); the boolean type, an example, boolean a; Booleans have only two values: true and false.

Reference Types There are three kinds of reference types: class types, interface types, and array types. In the following example of class declaration public class Demo { int x;} Demo is a class type, and x is a field (or an instance variables). How would you use the class type? To create an object. An object is instantiated (created) class. We need references to point to objects. Once we implement a class Demo, we implement a new type, so that we can declare Demo tmp;

An Example import javax.swing.JOptionPane; public class Example1 { public static void main(String[] args) { String s1, s2; int n1, n2, max; // prompt for integers s1 = JOptionPane.showInputDialog("Enter an integer"); s2 = JOptionPane.showInputDialog("Enter another integer"); // convert these strings to integers n1 = Integer.parseInt(s1); n2 = Integer.parseInt(s2); // find the maximum max = Math.max(n1,n2); // print the message and the max value JOptionPane.showMessageDialog(null, "The biggest is " + max); System.exit(0); }

Exceptions If the input string cannot be converted to an integer, showInputDialog() throw an exception. We will discuss exceptions in details much later. try { n1 = Integer.parseInt(s1); } catch (NumberFormatException a) { System.out.println("Tne number is not integer'); System.exit( 0 ); }

Operator new To create an object we use the new operator. Integer n; //declaration n = new Integer(5); //instantiation The act of creating an object is called instantiation. The above two steps can be done in one Integer n = new Integer(5); String str = new String("This is a string"); The new method calls a constructor, which is a special method of a class responsible for initialization.

Operator new Note, specially for String, you can avoid calling new. It's done implicitely. So the following operations are identical String str = "This is a string"; String str = new String("This is a string"); Here is another example. There is a class DecimalFormat in the package java.text which formats the unexact numbers. DecimalFormat precision = new DecimalFormat( "0.00" ); The class DecimalFormat has a method format, that formats the number to a particular form. Calling System.out.println(precision.format(1.2345) ); will print "1.23".

Control Flows if-statement as in c++ if (ch != 's' && total < MAX) count++; //same as count = count + 1; else count=/2; //same as count = count/2; block statements if (ch != 's' && total < MAX) { count++; System.out.println("for debugging"); } else count=/2;

Control Flows conditional operator as in c++. Here is the above example written as the conditional operator. count = (ch != 's' && total < MAX) ? count+1 : count/2; The conditional operator is a great tool to separate plural and single forms. Here is an example from the previous exercise (an exercise on converting miles to kilometers): JOptionPane.showMessageDialog(null, ml + " mile" + ((ml==1)?"":"s") + " is " + km + " kilometers")

Control Flows while-loop as in c++ while (true) // cannot be while(1) { System.out.println("a number"); System.exit(0); } One of the most common use of the while-loop is to validate input.

Control Flows switch-statement as in c++ switch (token) { case 'a': count1++; break; case 'b': count2++; break; default: System.out.println("error..."); }