Data Types in Java - Texas Tech University

Data Types in Java - Texas Tech University

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

the differences between interpreters and compilers Appletsinterpreted JAVA What or compiled? Apps is a namespace?

Is it supported by JAVA? A using keyword brings a namespace into scope 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 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 By clicking on references in the solution explorer box, you can see what assemblies have been selected for you Boxes in the VS 2008 IDE Code

and Text Editoris also the Forms Designer Solution Explorerupper right Properties Boxlower right XAML Editorlower middle Error List/Output box at the bottom Specifics Code and Text Editoralso can be used as the 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 Changing them in the Properties window in the lower right Changing them in the XAML window at the bottom Data Types Constants Variables

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

System.out.println(My First Java); // Java Console.writeline(My First C#); // Visual C# What is a variable? It is a named computer location in memory that holds values that might vary

Must that location have an address? YES What has addresses? Bits, bytes, words, what? Bytes Can a variable be more than one byte long?

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

Int the default declaration 4-byte integer Byte1-byte integer Short2-byte integer Long8-byte integer Floating Point Floata 4-byte floating point number Doublean 8-byte floating point

number There are eight primitive data types Name them 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 float data type?

The double data type? How long is the char data type? Primitives sizes and Ranges PRIMITIVE SIZE IN BITS RANGE int 32

-2 to the 31st to 2 to the 31st int 4 bytes 2147483648 long 64 -- 8 bytes

-2 to the 63rd to 2 to the 63rd float 32 +- 1.5 x 10^45 double 64 +- 5.0 x 10^324

decimal (C# only) 128 28 significant figures string 16 bits per char Not applicable

char 16 One character bool (boolean in Java) 8 True or false

The assignment operator A = = 36; Sets time Int

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

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

Static Void Mean??? 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?

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

} The above produces the following output C:\Java>_ C:\Java>Java NumbersPrintln Bills are sent on the 5th Next bill: October 5 C:\Java>_ This program would produce the same output Public class NumbersPrintln

{ public static void main(String[] args) { int billingDate = 5; System.out.println(Bills are sent on the + billingDate + th\nNext bill: October + billingDate); } } Simple Arithmetic Operators

* / % (multiplication, division, modulus) + - (addition, subtractionon a lower level of the precedence hierarchy) int result = 2 + 3 * 4; Is result 14 or 20?? int result = (2 + 3) * 4

Binary Operators The simple arithmetic operators are also called binary operators because they have two operands exactly Never three Never one Using the Boolean data type Boolean variables can hold only one of two valuestrue or false

Boolean isItPayday = false; Boolean areYouBroke = true; 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

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 Data formats The character formatuses an assigned decimal value The integer format The floating point formatconsists of an exponent part and a mantissa

partfor example the 4-byte floating point word might have a 1-byte exponent and a 3-byte mantissa. 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 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

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

the use of the (float), the code segment would not compile 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

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; In the latter two cases above The

char variables still hold a single character The backslash gives a new meaning to the character that follows The pair together represents a single nonprinting character To hold strings in a variable Use the string class that is built-in string firstName = Audrey; 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 users response is returned by the method and placed in a string variable 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); } } Using Methods, classes, and Objects Methods

are similar to procedures, functions, or subroutines Statements within a method execute only when the method is called To execute a method, you call it from another method The call calling method makes a method Simple methods.

Dont 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 Example

Public class First { Public static void main(String[] args) { System.out.println(First Java application); } } 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 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

Recently Viewed Presentations

  • International Project Management Day Webcast:

    International Project Management Day Webcast:

    Amends the Toxic Fire Retardant Prevention Act for FLAME RETARDANTS (polybrominated diphenyl ether) PBDE, penta-BDE, octa-BDE, Deca-BDE into PCs starting 1/1/2011. Hp packaging exceeds the 5 ppm in this bill in GSE for heavy metals at 100 ppm. PENNSYLVANIA H.B....
  • Explaining WWII with IR Theory

    Explaining WWII with IR Theory

    Explaining WWII with IR Theory ... Balance of Power caused WWI World needs reorientation of politics Realism failed, now try idealism/liberalism Prescription for Peace? Self-determination - freedom, break-down old empires (except France, Britain) Democracy Collective Security - League of ...
  • Warm Up  Name or Write the following compounds

    Warm Up Name or Write the following compounds

    Lewis Structures for Polyatomic Ions. When writing dot structures for polyatomic ions, you must remember to add or subtract the amount of electrons represented by the charge. When writing polyatomic ions, you must include the structure inside brackets, [ ],...


    unadjusted mortality of incident ESRD Veterans who transitioned to ESRD during 10/1/2007-3/31/2015 and who were followed for up to 36 months, stratified according to cause of hospitalization during transition to ESRD (N=89,527)
  • Eucharistic Visitor - Episcopal Diocese of El Camino Real

    Eucharistic Visitor - Episcopal Diocese of El Camino Real

    * Hospital Visits People who have illnesses that require acute medical care May be unable to receive communion Unless the patient is sitting up and alert, visits should be brief * Terminally Ill May be unable to receive communion Often...
  • Les lieux de la démocratie à Athènes au V siècle avant JC

    Les lieux de la démocratie à Athènes au V siècle avant JC

    Les lieux de la démocratie à Athènes au V° siècle avant JC Céramique Porte du Dipylon Agora Pnyx Acropole Théâtre de Dionysos Vers le Pirée
  • School Lab Safety -

    School Lab Safety -

    Demonstrate safety behavior and promote a culture of safety. Be proactive in every aspect of laboratory safety, making safety a priority. PPT-077-02. Teachers and teacher's-aides should lead by example: Wear personal protective equipment (PPE). Follow and enforce safety rules, procedures,...
  • Electrostatics and Coulombs Law

    Electrostatics and Coulombs Law 3 1 1 1 1 1 5 1 1 7 3 3 8 4 4 9 5 5 10 6 5 11 7 5 12 8 6 13 9 6 14 10 7 15 11 8 16 12 9...