Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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

2 “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.

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

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

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

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

7 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 10.123 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

8 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 }

9 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 {......}

10 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 * 3.1416; // step 3: display the area … System.out.println(area); }

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

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

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

14 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: { }

15 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


Download ppt "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."

Similar presentations


Ads by Google