Primitive Data Types byte, short, int, long float, double char boolean Are all primitive data types. Primitive data types always start with a small letter.

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

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Chapter 2 Elementary Programming
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example F Identifiers, Variables, and Constants F Primitive Data Types –byte,
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Declaring Variables You must first declare a variable before you can use it! Declaring involves: – Establishing the variable’s spot in memory – Specifying.
Constants Variables change, constants don't final = ; final double PI = ; … area = radius * radius * PI; see Liang, p. 32 for full code.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
A simple program: Area of a circle Problem statement: Given the radius of a circle, display its area Algorithm: 4. Display area To store values, we need.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Methods: a first introduction Two ways to make tea: 1: boil water; put teabag into cup;... etc : tell your younger brother: makeTea(1 milk, 0 sugar);
Fundamental data types Horstmann Chapter 4. Constants Variables change, constants don't final = ; final double PI = ; … areaOfCircle = radius *
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Java Overview CS2336: Computer Science II1. First Program public class HelloWorld { public static void main(String args[]) { System.out.println("Hello.
CS0007: Introduction to Computer Programming Scope, Comments, Code Style, Keyboard Input.
JOptionPane class. Dialog Boxes A dialog box is a small graphical window that displays a message to the user or requests input. A variety of dialog boxes.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
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
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.
Chapter 2 Elementary Programming
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Chapter 2: Java Fundamentals
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.
Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Introduction Chapter 1 8/31 & 9/1 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 2. Program Construction in Java. 01 Java basics.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
A Sample Program #include using namespace std; int main(void) { cout
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
Primitive Data Types and Operations F Introduce Programming with an Example F Identifiers, Variables, and Constants F Primitive Data Types –byte, short,
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.
Elementary Programming
Data types and variables
Type Conversion, Constants, and the String Object
5 Variables, Data Types.
An Introduction to Java – Part I, language basics
Chapter 2 Edited by JJ Shepherd
elementary programming
Anatomy of a Java Program
Java Programming Review 1
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Primitive Types and Expressions
Chapter 2 Primitive Data Types and Operations
Variables and Constants
Chapter 2: Java Fundamentals cont’d
Presentation transcript:

Primitive Data Types byte, short, int, long float, double char boolean Are all primitive data types. Primitive data types always start with a small letter. } Variables of primitive data types are just boxes in memory. Java gives us simple operations (like +, -, *, / etc) for doing things with primitive data types. We use casting to convert between primitive data types.

“Reference” data types String is not a primitive data type. String is a “reference” data type (it starts with a capital letter). Reference data types refer to (contain) a number of primitive elements combined together. For example, in String myName = “fintan”; The variable myName contains 6 char objects: the char s: ‘f’ ‘i’ ‘n’ ‘t’ ‘a’ ‘n’ Reference data types are very different from primitive types. They are not just boxes: they are Object s containing both data and methods (commands) for doing things with that data.

Method s for doing things with String s We have already used methods to do things: System.out.println(“Hi”); calls the println method (command) belonging to the out part of the computer System. We use a dot (. ) to identify a method belonging to something. String myName = “fintan”; int myNameLength = myName.length(); The length() method is a command inside the myName variable, that returns the length of (number of char s in) the String myName.

Calling String methods String myName = “fintan”; int myNameLength = myName.length(); This means: call the length() method belonging to (the dot) the String variable myName. This looks inside the String variable myName, counts the number of chars in myName, and returns that value as an int. String yourName = “mary”; int yourNameLength = yourName.length(); This looks inside the String variable yourName, counts the number of chars in yourName, and returns that value as an int.

More String methods To get the length of the string in a variable x, use x. length() and put the answer in an int variable. Method length() has no arguments (nothing between brackets). To get one of the char s in a String variable, use the charAt method in that variable. charAt counts char ’s from 0: String myName = “fintan”; char firstChar = myName.charAt(0); char secondChar = myName.charAt(1); char thirdChar = myName.charAt(2); After these commands, firstChar holds ‘f’ secondChar holds ‘i’ and thirdChar holds ‘n’

Converting numbers to Strings To convert primitive types to each other, we used a cast (that “squeezes” something from one box into another box). For more complex “reference” data types like String, things are more complicated: we need to use conversion commands (methods) held in a reference datatype. int i = 10; String anIntString = Integer.toString(i); This converts the value 10 in the int i into the string “10” and puts in in a String variable. Integer holds methods for converting int s to String s. Note the capital letter!!! And the variable i as an argument.

Converting String s to numbers Again, we use conversion methods in a “Reference” datatype: String s = “10.123”; double d; d = Double.parseDouble(s); Again, note the capital letter in the “reference” data type Primitive type Reference type holding methods intInteger floatFloat doubleDouble After this, the variable d contains the double parseDouble is a method that converts (parses) a string into a double. It’s stored in the Double reference type Reference types always start with capital letters

String conversion summary String s1 = “2”; String s2 = “10.123”; int i; float f; double d; i = Integer.parseInt(s1); f = Float.parseFloat(s2); d = Double.parseDouble(s2); s1 = Integer.toString(i); s2 = Float.toString(f); s2 = Double.toString(d); Strings to numbers: note String variables as arguments } Numbers back to strings: note number variables as arguments }

Documenting programs Add comments to programs to make them clearer /** * start each program with a block which * describes the program's purpose */ public class Foo {......}

More comments // a program to compute the area of a circle Public class ComputeArea { public static void main(String[] args) { double radius; double area; // step 1: store radius radius = 1.23; // step 2: compute, store area area = radius * radius * ; // step 3: display the area … System.out.println(area); }

Naming conventions Variables start with lower case. Capitalize subsequent words: int count; int thisIsAnotherCounter; Class names start with a capital letter public class MyFirstClass {....} Constants are all caps and underscores: final int DAYS_IN_WEEK = 7;

Indentation 1 public class MyClass { public static void main(String[] args) { int i = 1; System.out.println("Welcome to my program"); }} Unhelpful code:

Indentation 2 public class MyClass { public static void main(String[] args) { int i = 1; System.out.println("Welcome to my program"); } }

Indentation 3 Each block adds a new layer of indentation Indentation is an important habit.... … and leads to understandable code Block: statements surrounded by braces: { }

3 kinds of errors Syntax errors: caught by compiler. These are grammatical errors in program code Runtime errors: caught by the user when program is running. An example might be the user not entering a name in the Hello program Logic errors: Errors in the programs design. The program compiles and runs ok, but doesn’t do what is required in the problem statement