Pemrograman Dasar - Data Types1 THINGS & STUFF Identifier, Variable, Constant, Data type, operator, expression, assignment, javadoc.

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Declaring Variables You must first declare a variable before you can use it! Declaring involves: – Establishing the variable’s spot in memory – Specifying.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
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.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
String Escape Sequences
L EC. 02: D ATA T YPES AND O PERATORS (1/2) Fall Java Programming.
Java Chapter 2 Creating Classes. Class Defines structure of an object or set of objects; Includes variables (data) and methods (actions) which determine.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Chapter 2: Introducing Data Types and Operators.  Know Java’s primitive types  Use literals  Initialize variables  Know the scope rules of variables.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
Vladimir Misic: Java1 Week 2 – Java and more.
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
***** SWTJC STEM ***** Chapter 2-2 cg Identifiers - Naming Identifier, the name of a Java programming component. Identifier naming rules... Must.
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CPS120: Introduction to Computer Science
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
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.
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.
 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.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Java Overview. Comments in a Java Program Comments can be single line comments like C++ Example: //This is a Java Comment Comments can be spread over.
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++
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
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[]
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
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.
Review by Mr. Maasz, Summary of Chapter 2: Starting Out with Java.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Java Primer 1: Types, Classes and Operators
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Selenium WebDriver Web Test Tool Training
Type Conversion, Constants, and the String Object
Basic Java Syntax The Java language will be described by working through its features: variable types and expressions selection and iteration classes exceptions.
Java Programming: From Problem Analysis to Program Design, 4e
Escape Sequences What if we wanted to print the quote character?
Primitive and Reference Data Values
Chapter 2: Basic Elements of Java
Objects and Primitive Data
Chapter 2 Variables.
Data Types and Expressions
Java Programming Language
Chap 2. Identifiers, Keywords, and Types
Chapter 2: Java Fundamentals cont’d
Chapter 2 Primitive Data Types and Operations
Review of Java Fundamentals
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Pemrograman Dasar - Data Types1 THINGS & STUFF Identifier, Variable, Constant, Data type, operator, expression, assignment, javadoc

Pemrograman Dasar - Data Types2 Things & Stuff  Any program that you will write will manipulate things Numbers Strings Objects …  We need to be able to refer to these sorts of items in a program

Pemrograman Dasar - Data Types3 Identifiers  Identifiers are used in a programming language to refer to things You can think of an identifier as a shortcut to a memory location somewhere in the computer  Declarations in a program introduce identifiers and the type of thing they will refer to  All identifiers must be declared before they may be used

Pemrograman Dasar - Data Types4 Rules  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

Pemrograman Dasar - Data Types5 Declaring Variables  The basic syntax for declaring variables is: typename identifer;  It is possible to declare two or more variables of the same type in a single declaration statement.  int x, y, z ;

Pemrograman Dasar - Data Types6 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 or the value null (which is the null reference).  All variables must be declared and initialized before being used.

Pemrograman Dasar - Data Types7 Primitive Types  The primitive types represent the basic, built-in types that are part of the Java language.  Two basic categories: Boolean - boolean Numeric  Integer - byte, short, int, long, char  Floating point - float, double

Pemrograman Dasar - Data Types8 Primitive Types Note: these types are platform independent

Pemrograman Dasar - Data Types9 Constants  A constant is a variable that is declared as final  final double INT_RATE = 0.055; //5.5% Once it is given a value, it may not be changed  The use of named constants Is a good programming practice Makes it easy to identify and maintain Clarifies the meaning of a literal

Pemrograman Dasar - Data Types10 Declaring Constants Local to a method (a local constant) public void addInterest(){ final double RATE=0.055; … Private or public instance field (the constant can be different in each object) public class BankStuff{ private final double RATE=0.055; public final int SERIAL; … Private or public, but shared by all objects: only one copy of the constant exists; it is stored in the class (I.e. in the object factory) public class BankStuff{ public static final String ID="First National Bank"; …

Pemrograman Dasar - Data Types11 Using Static Members  Static members are referenced via the class name rather than an object name ClassName.staticMemberName  Examples System.out.print(BankStuff.ID); double twoPI = Math.PI*2;  You can access static members via an object, but this can be misleading

Pemrograman Dasar - Data Types12 Math Library Methods Square rootMath.sqrt(4)2.0 PowerMath.pow(2,5)32.0 Round to integer Math.round(4.7)5L FloorMath.floor(-3.1)-4.0D Absolute valueMath.abs(-4)-4 MaximumMath.max(-3,-8)-3 These are all static methods in the java.Math package

Pemrograman Dasar - Data Types13 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.

Pemrograman Dasar - Data Types14 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

Pemrograman Dasar - Data Types15 Unicode Special Character Escape Sequence NameUnicode Value \bBackspace\u0008 \tTab\u0009 \nLinefeed\u000a \rCarriage return\u000d \”Double quote\u0022 \’Single quote\u0027 \\backslash\u005c

Pemrograman Dasar - Data Types16 Literals

Pemrograman Dasar - Data Types17 Assignment  Declarations associates a type with an identifier  Assignment associates a value with an identifier Assignment is represented by an = sign An identifier will always be on the left side of the equals sign The computer will place a copy of the thing on the right into the area named by the identifier on the left  Assignment is not the same as algebraic equality

Pemrograman Dasar - Data Types18 Examples int aVariable; aVariable = 67; float pi = 3.14; char a, b, c; a=‘a’; b=‘b’; c=‘c’; char a=‘1’, b=‘2’, c=‘3’;

Pemrograman Dasar - Data Types19 Expressions  You can also assign values to variables using arithmetic expressions int a = / 5;  In this case the computer will evaluate (figure out) the value of the equation on the right and assign that value to the variable a  This is an example of an arithmetic expression

Pemrograman Dasar - Data Types20 Operators

Pemrograman Dasar - Data Types21 Precedence  What value is assigned to a below? int a = / 2  A computer language must not be ambiguous  The precedence rules define the order in which operations are evaluated in an expression Parenthesis can always be used to make the order clear

Pemrograman Dasar - Data Types22 Operators Presedence

Pemrograman Dasar - Data Types23 Mixed Mode Expressions  What happens if an expression contains two different types of numbers? int a = *.56;  In most cases Java will automatically convert the values in the expression so that it may be evaluated

Pemrograman Dasar - Data Types24 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

Pemrograman Dasar - Data Types25 Manual Type Conversion  In some situations Java will not perform automatic conversions int x = ;  In these cases you can force a conversion by specifying a cast int x = (int)3.1456;  Here information is lost, but the assignment will take place

Pemrograman Dasar - Data Types26 Reference Types  Reference types are used to declare variables that will refer to objects  The JDK provides a number of classes The String class allows us to declare, create, and manipulate strings  Declaring a string is no different from declaring a primitive type: String name;

Pemrograman Dasar - Data Types27 Creating Objects  Before a reference to an object may be assigned to a variable, the object must be created Operator new is used to create new objects String name = new String(); String name = new String( “Paul Tymann” ); String name = “Paul Tymann”;

Pemrograman Dasar - Data Types28 References  Variables refer to objects, they do not contain the object  Several different variables may all refer to the same object  If an object is not referred to by any variables, the object will eventually be destroyed

Pemrograman Dasar - Data Types29 Methods  A reference to an object can be used to invoke a method of the object The dot (.) operator specifies method invocation String name = “Paul Tymann”; System.out.println( name.substring( 6 ) );  An attempt to invoke a method using a null reference is an error

Pemrograman Dasar - Data Types30 What Methods?  How do you know what methods are available for a given object? Look at the class definition Look at the documentation for the class  The JDK provides documentation for its classes using a tool called JavaDoc

Pemrograman Dasar - Data Types31 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 symbol and should start on a new line.

Pemrograman Dasar - Data Types32 Javadoc Tags

Pemrograman Dasar - Data Types33 Example /** * A class that manages a circle given the radius java.lang.Math 1.0 Paul Tymann */ public class Circle { private double radius; /** * Constructor for a circle. * radius radius of the circle being created. Must be * positive and greater than 0. * */ public Circle( double radius ) { this.radius = radius; } /** * A class that manages a circle given the radius java.lang.Math 1.0 Paul Tymann */ public class Circle { private double radius; /** * Constructor for a circle. * radius radius of the circle being created. Must be * positive and greater than 0. * */ public Circle( double radius ) { this.radius = radius; }

Pemrograman Dasar - Data Types34 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.  The 1.1 version of Javadoc did not support local modifications to the java documentation well.  A much improved version of Javadoc is provided with java2.

Pemrograman Dasar - Data Types35 The Result