Java: Base Types All information has a type or class designation

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
String Escape Sequences
Java Chapter 2 Creating Classes. Class Defines structure of an object or set of objects; Includes variables (data) and methods (actions) which determine.
***** SWTJC STEM ***** Chapter 2-2 cg Identifiers - Naming Identifier, the name of a Java programming component. Identifier naming rules... Must.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
From C++ to Java A whirlwind tour of Java for C++ programmers.
Chap. 1 Classes, Types, and Objects. How Classes Are Declared [ ] class [extends ] [implements,, … ] { // class methods and instance variable definitions.
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.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Objects, Classes and Syntax Dr. Andrew Wallace PhD BEng(hons) EurIng
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
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.
Some Standard Classes Goals The Object class The String class Wrapper classes The Math class Random Numbers.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Copyright Curt Hill Variables What are they? Why do we need them?
Objects and Classes Mostafa Abdallah
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
CPS APTs and structuring data/information l Is an element in an array, Where is an element in an array?  DIY: use a loop  Use Collections, several.
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Utilities (Part 2) Implementing static features 1.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
CPS What's in Compsci 100? l Understanding tradeoffs: reasoning, analyzing, describing…  Algorithms  Data Structures  Programming  Design l.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
CompSci 100E JB1.1 Java Basics (ala Goodrich & Tamassia)  Everything is in a class  A minimal program: public class Hello { public static void main(String[]
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
CompSci Toward understanding data structures  What can be put in a TreeSet?  What can be sorted?  Where do we find this information?  How do.
CompSci Announcements  Read Chap 3 for next time  Homework will be posted  Consulting Hours will start soon.
More on Arrays Review of Arrays of ints, doubles, chars
Chapter 2 Variables.
Objects as a programming concept
Java: Base Types All information has a type or class designation
Java Variables and Types
String class.
Java Primer 1: Types, Classes and Operators
Multiple variables can be created in one declaration
Computer Science 3 Hobart College
Programming Language Concepts (CIS 635)
Primitive Types Vs. Reference Types, Strings, Enumerations
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
An Introduction to Java – Part I, language basics
Implementing Non-Static Features
Chapter 2 Variables.
Chapter 2: Java Fundamentals
Recap Week 2 and 3.
Python Primer 1: Types and Operators
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Java Programming Language
Web Design & Development Lecture 4
Classes CS 21a: Introduction to Computing I
In this class, we will cover:
Names of variables, functions, classes
Chapter 2 Variables.
Just Enough Java 17-May-19.
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Java String Class String is a class
Variables and Constants
Presentation transcript:

Java: Base Types All information has a type or class designation Built-in Types Called : primitive types or base types boolean, char, bye, short, int, long, float, double Primarily use: int, double, boolean Need to declare before using; defined (created) when declared int x, y;// declaration only ? Initialization // some people group declarations at front x = 39; // use y = 3 + x; int num = 10; // declaring when first used Each type has its limitations int: whole numbers only, limited range double: implements scientific notation: fractions and wider range boolean: true or false values only

Java: Operators for Base Types Familiar operators available: +, -, *, /, % For ints, / yields whole number quotient % yields the remainder What are 12/5 ? 13%5 ? What is the meaning of (2*pancakes + capacity -1)/capacity*5? Also have comparison operators: <, <=, ==, !=, >=, > Do not use = (assignment) where you mean == Learn about operator precedence from text (p22) Most pretty intuitive – well designed language However, when in doubt, use parenthethes Most of these operators have no meaning for objects

Java: Objects Objects are instantiations of a class Use new operator to create Invokes constructor which initializes object Assume we have class DLicense (Class names start with capital letter) DLicense john; // declare (Object names (e.g. john) start with lower case letter) john = new DLicense(); // create DLicense susan = new DLicense( ); //combine DLicense bud = new License(”Ted Bud”,”6/4/1989”); Creation may allow or require arguments Depends on constructor(s)

Java: Classes Combine data and function in one package Data Methods Use new operator to create Invokes constructor which initializes object Data Store in instance variables (fields) May be primitive types or May be objects Methods Often called functions, subroutines, or procedures Usually do things to and with the instance variables

Java: Methods (Functions) Classes usually define one or more methods Structure: access return_type name(parameters) { // method body } Accessor methods return info Return_type indicates type of info returned Often have empty parameter list Mutator methods change state Usually parameters are involved in making the changes Often no info returned, thus return_type is void (Object names (e.g. john) start with lower case letter) Invoking methods Use: object.method(params); object is implicit parameter

Java: Sample Class public class DLicense { private String name; private String dob; private double height; public DLicense() { // constructor name = ””; dob = ””; height = 0.0; } public DLicense(String nm, String bd){ name = nm; dob = bd; // methods to follow

Java: Sample Class (cont) public String getName(){ return name; } public String getDOB() { return dob; public double getHeight() { return height; public void setHeight(double ht) { height = ht; public String display() { return name + ” ” + height + ”\” born: ” + dob;

Java: Sample Class (cont) public static void main(String[] args) { DLicense john; john = new DLicense(); john.setName(”John Smith”); john.setHeight(72.0); DLicense susan = new DLicense(); susan.setName(”Sue Peggy”); susan.setHeight(66.5); DLicense bud = new License(”Ted Bud”,”6/4/1989”); bud.setHeight(68.0); System.out.println(bud.display()); System.out.println(john.getName()); System.out.println(susan.getHeight()); }

Java Strings String is a class Can join strings (concatenate) with + Do not need new to create String String msg = ”hello”; String constants (literals) use ”, not ’ Can join strings (concatenate) with + String mail = ”John says ” + msg; Most common String methods: int length(); // get number of chars in it String substring(int start, int stop); // substring gets part of string int indexOf(String key); //finds loc of key

Java Control of Flow (by example) if if (a > b) { msg = ”good”; } if-else if (w.hasMore()) { System.out.println(w.next()); count = count + 1; else System.out.println(”All done”);

Java Control of Flow (by example) while n = 10; while (n > 0){ System.out.println(”down to ” + n); n = n – 1; } for for (n = 10; n > 0; n--) do-while do { } while (n > 0);

What can an Object do (to itself)? http://www.cs.duke.edu/csed/java/jdk1.4/docs/api/index.html Look at java.lang.Object toString() Used to print (System.out.println) an object, overriding toString() can result in 'useful' information being printed, also used in String concatenation: String s = x + y; Default is basically a pointer-value equals() Determines if guts of two objects are the same, must override, e.g., for using a.indexOf(o) in ArrayList a Default is ==, pointer equality hashCode() Hashes object (guts) to value for efficient lookup

What's in the boxes? "genome" is in the boxes Objects and values Primitive variables are boxes think memory location with value Object variables are labels that are put on boxes String s = new String("genome"); String t = new String("genome"); if (s == t) {they label the same box} if (s.equals(t)) {contents of boxes the same} s t What's in the boxes? "genome" is in the boxes

Objects, values, classes For primitive types: int, char, double, boolean Variables have names and are themselves boxes (metaphorically) Two int variables assigned 17 are equal with == For object types: String, Sequence, others Variables have names and are labels for boxes If no box assigned, created, then label applied to null Can assign label to existing box (via another label) Can create new box using new Object types are references or pointers or labels to storage