0% found this document useful (0 votes)
21 views

Data Types

Uploaded by

Triziah Acosta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Data Types

Uploaded by

Triziah Acosta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 39

Data Types in Java

Data Types
 Constants
 Variables
What is a Constant?
 456—a 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
 Byte—1-byte integer
 Short—2-byte integer
 Long—8-byte integer
Floating Point
 Float—a 4-byte floating point number
 Double—an 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 a = to the constant 36 at execution time
 Int 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
 TheMain method
 Why??
 It
is used as an entry point to the program for
some types of programs.
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, subtraction—on 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 values—true 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 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.
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 user’s 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 calling method makes a method call”
Simple methods….
 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
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
Variable Scope
 A variable’s scope starts at declaration, extends to
nested blocks, and ends at the end of the block it was
declared in.
 A blocks local variables are variables declared within that
block of code, but do not include variables declared in
nested blocks.
 A class’s fields can be hidden by local variables or
method parameters, but this is generally a bad idea
unless the variable is a copy.
Constructors

• A constructor is a special member function


whose task is to initialize the objects of its
class.
• It is special because its name is same as
the class name.
• The constructor is invoked whenever an
object of its associated class is created.
• It is called constructor because it constructs
the values of data members of the class.
ASSIGNMENT:
What do the keywords
 Public
 Static
 Void

 Mean???

You might also like