Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Types in Java James Burns. Recitation Name some characteristics of objects Name some characteristics of objects Chemical Bank Chemical Bank Describe.

Similar presentations


Presentation on theme: "Data Types in Java James Burns. Recitation Name some characteristics of objects Name some characteristics of objects Chemical Bank Chemical Bank Describe."— Presentation transcript:

1 Data Types in Java James Burns

2 Recitation Name some characteristics of objects Name some characteristics of objects Chemical Bank Chemical Bank Describe the differences between interpreters and compilers Describe the differences between interpreters and compilers Applets—interpreted or compiled? Applets—interpreted or compiled? JAVA Apps— JAVA Apps— What is a namespace? What is a namespace? Is it supported by JAVA? Is it supported by JAVA? A using keyword brings a namespace into scope A using keyword brings a namespace into scope

3 Four common namespaces (C#) using System; using System.Collections.Generic; using System.Linq; using System.Text; There are hundreds of classes in these namespaces The.NET framework class library contains many thousands of classes

4 Namespaces are also called assemblies of classes When you select a particular template type upon creation of a project, that results in references to the appropriate assemblies being included automatically for you When you select a particular template type upon creation of a project, that results in references to the appropriate assemblies being included automatically for you By clicking on references in the solution explorer box, you can see what assemblies have been selected for you By clicking on references in the solution explorer box, you can see what assemblies have been selected for you

5 Boxes in the VS 2008 IDE Code and Text Editor—is also the Forms Designer Code and Text Editor—is also the Forms Designer Solution Explorer—upper right Solution Explorer—upper right Properties Box—lower right Properties Box—lower right XAML Editor—lower middle XAML Editor—lower middle Error List/Output box at the bottom Error List/Output box at the bottom

6 Specifics Code and Text Editor—also can be used as the forms designer Code and Text Editor—also can be used as the forms designer Use ICONS above Solution Explorer on the right to go from code editor to forms designer Use ICONS above Solution Explorer on the right to go from code editor to forms designer Select an object on the form and change its properties by Select an object on the form and change its properties by Changing them in the Properties window in the lower right Changing them in the Properties window in the lower right Changing them in the XAML window at the bottom Changing them in the XAML window at the bottom

7 Data Types Constants Constants Variables Variables

8 What is a Constant? 456—a literal numerical constant 456—a literal numerical constant System.out.println(456); // Java System.out.println(456); // Java Console.writeline(456); // Visual C# Console.writeline(456); // Visual C# “A Literal String Constant” “A Literal String Constant” System.out.println(“My First Java”); // Java System.out.println(“My First Java”); // Java Console.writeline(“My First C#”); // Visual C# Console.writeline(“My First C#”); // Visual C#

9 What is a variable? It is a named computer location in memory that holds values that might vary It is a named computer location in memory that holds values that might vary Must that location have an address? Must that location have an address? YES YES What has addresses? Bits, bytes, words, what? What has addresses? Bits, bytes, words, what? Bytes Bytes Can a variable be more than one byte long? Can a variable be more than one byte long? YES YES

10 Data type Declarations Specify the type of data and the length of the data item in bytes Specify the type of data and the length of the data item in bytes int, short, long int, short, long float, double float, double boolean boolean char char

11 Data Types -- Integer Int – the default declaration – 4-byte integer Int – the default declaration – 4-byte integer Byte—1-byte integer Byte—1-byte integer Short—2-byte integer Short—2-byte integer Long—8-byte integer Long—8-byte integer

12 Floating Point Float—a 4-byte floating point number Float—a 4-byte floating point number Double—an 8-byte floating point number Double—an 8-byte floating point number

13 There are eight primitive data types Name them Name them Boolean, byte, char, double, float, int, long, short Boolean, byte, char, double, float, int, long, short In bytes, how long is the short data type? The int data type, the long data type? In bytes, how long is the short data type? The int data type, the long data type? In bytes, how long is the float data type? The double data type? In bytes, how long is the float data type? The double data type? How long is the char data type? How long is the char data type?

14 Primitives sizes and Ranges PRIMITIVESIZE IN BITSRANGE int32-2 to the 31 st to 2 to the 31 st int4 bytes2147483648 long64 -- 8 bytes-2 to the 63 rd to 2 to the 63rd float32+- 1.5 x 10^45 double64+- 5.0 x 10^324 decimal (C# only)12828 significant figures string16 bits per charNot applicable char16One character bool (boolean in Java)8True or false

15 The assignment operator = A = 36; A = 36; Sets a = to the constant 36 at execution time Sets a = to the constant 36 at execution time Int A =36; Int A =36; Sets A = to the constant 36 at compile time Sets A = to the constant 36 at compile time Initializes A to 36 at the time memory is set aside for it Initializes A to 36 at the time memory is set aside for it

16 Name a Method that many Java classes have The Main method The Main method Why?? Why?? It is used as an entry point to the program for some types of programs. It is used as an entry point to the program for some types of programs.

17 What do the keywords Public Public Static Static Void Void Mean??? Mean???

18 Which of these do we usually use in connection with a class? Which of these do we use in connection with the declaration of a main? Which of these do we use in connection with the declaration of a main?

19 What is concatenation?

20 Consider the following: Public class NumbersPrintln { public static void main(String[] args) public static void main(String[] args) { int billingDate = 5; int billingDate = 5; System.out.print(“Bills are sent on the “); System.out.print(“Bills are sent on the “); System.out.print(billingDate); System.out.print(billingDate); System.out.println(“th”); System.out.println(“th”); System.out.println(“Next bill: October “ + billingDate); System.out.println(“Next bill: October “ + billingDate); }}

21 The above produces the following output C:\Java>_ C:\Java>Java NumbersPrintln Bills are sent on the 5 th Next bill: October 5 C:\Java>_

22 This program would produce the same output Public class NumbersPrintln { public static void main(String[] args) public static void main(String[] args) { int billingDate = 5; int billingDate = 5; System.out.println(“Bills are sent on the “ + billingDate + “th\nNext bill: October “ + billingDate); System.out.println(“Bills are sent on the “ + billingDate + “th\nNext bill: October “ + billingDate); }}

23 Simple Arithmetic Operators * / % (multiplication, division, modulus) * / % (multiplication, division, modulus) + - (addition, subtraction—on a lower level of the precedence hierarchy) + - (addition, subtraction—on a lower level of the precedence hierarchy) int result = 2 + 3 * 4; int result = 2 + 3 * 4; Is result 14 or 20?? Is result 14 or 20?? int result = (2 + 3) * 4 int result = (2 + 3) * 4

24 Binary Operators The simple arithmetic operators are also called binary operators because they have two operands exactly The simple arithmetic operators are also called binary operators because they have two operands exactly Never three Never three Never one Never one

25 Using the Boolean data type Boolean variables can hold only one of two values—true or false Boolean variables can hold only one of two values—true or false Boolean isItPayday = false; Boolean areYouBroke = true;

26 Comparison operators The result is boolean, always < less than > greater than == equal to <= less than or equal to >= greater than or equal to != not equal to

27 Boolean examples boolean is SixBigger = (6 > 5); // value stored in is SixBigger is true Boolean is SevenSmaller = (7 <= 4); // value stored in is SevenSmaller is false

28 Data formats The character format—uses an assigned decimal value The integer format The floating point format—consists of an exponent part and a mantissa part—for example the 4-byte floating point word might have a 1-byte exponent and a 3- byte mantissa.

29 What happens when you try to do arithmetic with different data types? The lower-level data type is converted to the higher-level data type before the binary operation is performed 1. double 2. float 3. long 4. int

30 Example int hoursWorked = 37; Double payRate = 6.73; Double grossPay = hoursWorked * payRate; Here, hoursWorked is converted from int to double before the * operation is performed; the result, grossPay contains 249.01 stored as a double

31 Type casting Forces a value of one data type to be used as a value of another type Forces a value of one data type to be used as a value of another type Example Example Double bankBalance = 189.66; Float weeklyBudget = (float) bankBalance / 4; /* weeklyBudget is 47.415, one-forth of bankBalance */

32 In the above… Without the use of the (float), the code segment would not compile Without the use of the (float), the code segment would not compile

33 Another type casting example float myMoney = 47.82f; int dollars = (int) myMoney; // dollars is 47, the integer part of myMoney // note that myMoney was not rounded

34 The char data type Holds only a single character Legal Examples char myMiddleInitial = ‘M’; char myGradeInChemistry = ‘A’; char aStar = ‘*’; char aCharValue = ‘9’; char aNewLine = ‘\n’; char aTabChar = ‘\t’;

35 In the latter two cases above… The char variables still hold a single character The char variables still hold a single character The backslash gives a new meaning to the character that follows The backslash gives a new meaning to the character that follows The pair together represents a single nonprinting character The pair together represents a single nonprinting character

36 To hold strings in a variable… Use the string class that is built-in string firstName = “Audrey”;

37 Using the Joption Pane Class for GUI Input An input dialog box asks a question and provides a text field in which the user can enter a response. The user’s response is returned by the method and placed in a string variable

38 An example Import javax.swing.JOptionPane; Public class HelloNameDialog { Public static void main(string[] args) { String result; result = JOptionPane.ShowInputDialog(“What is your name?”); JOptionPane.showMessageDialog(null, “Hello, “ + result + “!”); System.exit(0);}}

39

40 Using Methods, classes, and Objects Methods are similar to procedures, functions, or subroutines Methods are similar to procedures, functions, or subroutines Statements within a method execute only when the method is called Statements within a method execute only when the method is called To execute a method, you call it from another method To execute a method, you call it from another method “The calling method makes a method call” “The calling method makes a method call”

41 Simple methods…. Don’t require any data items (arguments or parameters), nor do they return any data items back Don’t require any data items (arguments or parameters), nor do they return any data items back You can create a method once and use it many times in different contexts You can create a method once and use it many times in different contexts

42 Example Public class First { Public static void main(String[] args) { System.out.println(“First Java application”); }}

43 Method Declaration Is the first line or header of a method and contains Optional access modifiers The return type for the method The method name An opening parenthesis An optional list of method arguments separated by commas A closing parenthesis

44 Access Modifiers public – accessible anywhere private – accessible only within the class in which it is defined protected – allows members of a derived class to access members of its parent classes static – does not require instantiation before it can be used and remains in place after use, without being destroyed


Download ppt "Data Types in Java James Burns. Recitation Name some characteristics of objects Name some characteristics of objects Chemical Bank Chemical Bank Describe."

Similar presentations


Ads by Google