Java Output and Data Types
description
Transcript of Java Output and Data Types
![Page 1: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/1.jpg)
![Page 2: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/2.jpg)
College BoardA.P. Computer Science A Topics
Program Design - Read and understand a problem's description, purpose, and goals.
Procedural Constructs - Constant declarations ; Variable declarations
Standard Data Stuctures - Simple data types (int, boolean, double)
![Page 3: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/3.jpg)
Vocabulary & Terms
Reference Variable Keyword Identifier Data type int char double
boolean Object Initializing a variable Assignment operator
![Page 4: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/4.jpg)
Basic Output Commands
print()println()
![Page 5: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/5.jpg)
System.out.print("Hello”);
object / reference
command / method
OUTPUTHello
System is the class name.out is an object that prints characters on the console window (black window on the screen). • println is a method, or behavior, that the System.out object carries out. • The characters enclosed in double quotation marks and are called a string. The string is printed in the console window. • Each statement in a program ends with a semicolon (;).
Try this in the interactions pane of Dr. Java. You should get the output to the left.
![Page 6: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/6.jpg)
Type the following into java:public class TestOutputs{ public static void main(String [] args) { System.out.print("Hello"); System.out.print("Hello"); }}
OUTPUTHelloHello
![Page 7: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/7.jpg)
public class TestOutputs{ public static void main(String [] args) { System.out.println("Hello"); System.out.print("Hello"); }}
OUTPUTHelloHello
![Page 8: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/8.jpg)
Try the statements below in your TestOutputs program. Be sure to run each one and not any differences. Pay close attention to the difference between print and println.
![Page 9: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/9.jpg)
Java provides a facility for displaying special characters in a string. These special characters are indicated by placing a backslash (\) in the string. For example, since double quotation marks are used to indicate the start and end of a string, there is no obvious way to include quotation marks within a string.
Therefore, the backslash must be used as in the statement:
System.out.println(“You say \”Goodbye,\” and I say \“Hello.\””);
Try this in the TestOutputs program. It should display:
You say "Goodbye," and I say "Hello.“
The double quotation marks that follow the backslashes are displayed rather than interpreted as ending the string. The combination of symbols is called an escape character. The idea is that it escapes from the normal interpretation of characters in a string.
![Page 10: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/10.jpg)
Another special escape character is the end-of-line character. This is indicated by \n and can replace the use of –ln in println. For example:
System.out.print(“This is the first line,\n”); System.out.print(“and this is the second line.”);
Try this in the TestOutputs program.
Displays:This is the first line, and this is the second line.
![Page 11: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/11.jpg)
System.out.println("\\hello\"/");
\\ Prints out \\“ Prints out “\’ Prints outs ’\n Goes to next line\t Tab(moves over 8 spaces
OUTPUT\hello"/
![Page 12: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/12.jpg)
What is the output?(Attempt to answer the questions first, then type each into the interactions pane in Dr. Java to confirm your answers).
System.out.println( “h\tello”);
System.out.println( “hel\\lo\””);
System.out.println( “hel\nlo”);
![Page 13: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/13.jpg)
What is a data type?
![Page 14: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/14.jpg)
byte short intlong float doublechar boolean
int num = 9;double total = 3.4;
We will use int, double and boolean in this course.
![Page 15: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/15.jpg)
![Page 16: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/16.jpg)
TYPE SIZE RANGE
byte 8 bits -128 to 127
short 16 bits -32768 to 32767
int 32 bits -2 billion to 2 billion
long 64 bits -big to +big
![Page 17: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/17.jpg)
Think about a light bulb. It has 2 states ON or OFF. The same principals can be applied to a computer. The electronic circuits are either On or Off. Different combinations of On and Off mean different things for the computer.
For the computer On is 1, and Off is 0. A number system made up of 1’s and 0’s is known as the Binary Number System. We use the Decimal Number System: digits 0 – 9.
![Page 18: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/18.jpg)
What is a BIT?
Each 1 or 0 is called a BIT. It is short hand… take the b at the start of binary and the it at the end of digit… BIT = Binary digIT.
Memory consists of bits and bytes.1 byte = 8 bits
Every character on the keyboard has a binary digit associated with it in accordance with the UNICODES.
Look at the following site: http://www.neurophys.wisc.edu/comp/docs/ascii/#table
What is the Binary Number for the letter A? What about a? 7?
The more bits you have the more you can store.
![Page 19: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/19.jpg)
int one = 120; int two = 987123;
System.out.println(one);System.out.println(two);
(Type the code above into the interactions pane in Dr. Java and check your output.)
OUTPUT120987123
![Page 20: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/20.jpg)
Type the following code into Dr. Java exactly as it is written. Compile the code and run the program. Are the results what you expected?
//integer example
public class Integers{
public static void main(String args[]){
int one = 120; //legal assignmentint two = 987123;int three = 999999999;
System.out.println(one);System.out.println(two);
three = three * 3; //creates an overflow error at runtimeSystem.out.println(three);
}}
![Page 21: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/21.jpg)
![Page 22: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/22.jpg)
TYPE SIZE RANGE
float 32 bits -big to +big
double 64 bits -big to +big
Float has 7 digits of precision and doublehas 15 digits of precision.
We are talking about decimal numbers now!int can NOT store decimal numbers.
![Page 23: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/23.jpg)
double one = 99.57;double two = 3217;double three = 23.32;
System.out.println(one);System.out.println(two);System.out.println(three);
(type the code above into the interactions pane in Dr. Java and check your output.)
OUTPUT99.573217.023.32
![Page 24: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/24.jpg)
Type the following program into Dr. Java exactly as it is written. Compile the code and then run the program. Are the results what you would have expected?
//real number example
public class Reals{
public static void main(String args[]){
double one = 99.57;double two = 3217;
System.out.println(one);System.out.println(two);
}}
![Page 25: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/25.jpg)
![Page 26: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/26.jpg)
Characters store letters.
char let = ‘A’;
char is a 16-bit unsigned integer data type.
We use single quotes for character literals.We use double quotes for String literals… morethan 1 character.
![Page 27: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/27.jpg)
char is a 16-bit unsigned int data type.
For example, here is a 16 bit pattern: 000000000110011
char let = 65;
ASCII VALUES YOU SHOULD KNOW!!!‘A’ – 65‘a’ – 97‘0’ - 48
![Page 28: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/28.jpg)
char alpha = 'A';char ascii = 65;char sum = 'B' + 1;
System.out.println(alpha);System.out.println(ascii);System.out.println('B'+1);
//’B’ gets converted to its ASCII value //66 and then 1 is added to it.
System.out.println(sum);//since sum holds 67 and it is a char, we //get C.
OUTPUTAA67C
![Page 29: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/29.jpg)
Type the following program in Dr. Java exactly as it is written. Compile the code and run the program. Are the results what you expected?
public class Char{
public static void main(String args[]){
char alpha = 'A';char ascii = 65;char sum = 'B' + 1;
System.out.println(alpha);System.out.println(ascii);System.out.println('B'+1); //char is an integer typeSystem.out.println(sum);
}}
![Page 30: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/30.jpg)
![Page 31: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/31.jpg)
The boolean data type can store trueor false only.
boolean heads = true;
boolean tails= false;
![Page 32: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/32.jpg)
Type the following program into Dr. Java exactly as it is written. Compile the code and run the program. Are the results what you expected?
public class BooleanTest{
public static void main(String args[]){
boolean stop = true;boolean go = false;
System.out.println( stop );
stop = go;
System.out.println( go );System.out.println( stop );
}}
![Page 33: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/33.jpg)
In JAVA, you have 8 primitive data types that take up very little memory.
Everything else in Java in an Object.Strings are objects.
String temp = "abc";
![Page 34: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/34.jpg)
![Page 35: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/35.jpg)
When we have values that should not change during the program, we make them constants.
Use the keyword final to create a constant.
Constants should be assigned a value upon creation.
final int birthYear = 1976;
For this program, birthYear will always be set to 1976.
![Page 36: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/36.jpg)
![Page 37: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/37.jpg)
Strings are collections of characters. They are words, sentences, combinations of letters and numbers, etc.
When declaring and creating an instance of any class, we follow this pattern:
ClassName objectName = new ClassName(parameters);
String name = new String (“Furman”);
String literals: Actual Strings enclosed in double quotes.
![Page 38: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/38.jpg)
The String class is a special class. It is used so much, that we have some exceptions for how they can be created.
String name = new String (“Furman”);
OR
String name = “Furman”; //whenever Java comes across a String literal,
it automatically creates a String object.
The first part String name declares name as a variable to hold a String object.The second part = “Furman”; or = new (“Furman”); creates a String object, and gives name the memory address.
![Page 39: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/39.jpg)
![Page 40: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/40.jpg)
receiver = 57;
receiver is assigned( = ) the value 57. In an assignment statement, the receiver is always on the left of the assignment symbol ( = ).
Assignment statements examples :
total = 10; amount = 100.34;
![Page 41: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/41.jpg)
You can initialize more than one variable in one statement if they have the same type. Just separate them using a comma.
int number = 75, it=99;
float taxrate = 7.75;
char letter = ‘A’, newlet = ‘a’;
boolean isprime = false;
String sone = "abc";
![Page 42: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/42.jpg)
![Page 43: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/43.jpg)
Java is a strong typed language. As a result, you must be very careful to look at datatypes when assigning values.
int one=90;char letter= ‘A’;char let= 65;
one=letter; letter=let; one=let;
![Page 44: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/44.jpg)
int one = 90;double dec = 234.5;char letter = 'A';
System.out.println( one );one = letter; //char to intSystem.out.println( one );
one = 'A'; //char to intSystem.out.println( one );
System.out.println( dec );dec = one; //int to doubleSystem.out.println( dec );
System.out.println (one);one = dec; //double to int… IllegalSystem.out.println (one);
OUTPUT906565234.565.065Error
![Page 45: Java Output and Data Types](https://reader035.fdocuments.net/reader035/viewer/2022081511/56814c1f550346895db923c8/html5/thumbnails/45.jpg)
Type the following program into Dr. Java. Compile the code and run the program. Are the results what you expected?
//strong typed language example
public class MixingData{
public static void main(String args[]){
int one = 95;double dec = 12.4;char letter = ‘B';
System.out.println( one );
one = letter; //char to intSystem.out.println( one );
one = 'A'; //char to intSystem.out.println( one );
System.out.println( dec ); dec = one; //int to double
System.out.println( dec );
System.out.println( letter );
//letter = dec; //double to int - not legal//System.out.println( letter );
}}