Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Pemrograman Dasar - Data Types1 THINGS & STUFF Identifier, Variable, Constant, Data type, operator, expression, assignment, javadoc."— Presentation transcript:

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

2 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

3 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

4 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

5 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 ;

6 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.

7 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

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

9 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

10 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"; …

11 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

12 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

13 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.

14 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

15 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

16 Pemrograman Dasar - Data Types16 Literals

17 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

18 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’;

19 Pemrograman Dasar - Data Types19 Expressions  You can also assign values to variables using arithmetic expressions int a = 1 + 4 / 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

20 Pemrograman Dasar - Data Types20 Operators

21 Pemrograman Dasar - Data Types21 Precedence  What value is assigned to a below? int a = 2 + 4 / 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

22 Pemrograman Dasar - Data Types22 Operators Presedence

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

24 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

25 Pemrograman Dasar - Data Types25 Manual Type Conversion  In some situations Java will not perform automatic conversions int x = 3.1456;  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

26 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;

27 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”;

28 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

29 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

30 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

31 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 an @ symbol and should start on a new line.

32 Pemrograman Dasar - Data Types32 Javadoc Tags

33 Pemrograman Dasar - Data Types33 Example /** * 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; } /** * 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; }

34 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.

35 Pemrograman Dasar - Data Types35 The Result


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

Similar presentations


Ads by Google