Title: Today were going to cover some new Java features and review some old ones
1Today were going to cover some new Java features
and review some old ones
- Review of binary (base 2) and look at octal (base
8) and hexadecimal (base 16) number systems - Refresher on primitive data types, Objects, and
autoboxing - Refresher on generics, which we used with
ArrayLists last year - Refresher on the for-each loop
- Introduction to C-style printf (for formatted
output, useful for Code Wars type stuff) - Introduction to C style enums
- Introduction to static imports (which are stupid)
2Base 2, 8, 10, and 16 Number Systems
3Base 10 (decimal)
- With decimal numbers, each place in a number has
a set value - 525
52510 5x100 2x10 5x1
102 101 100
100's 10's 1's
4Base 2 (binary)
10102 1x8 0x4 1x2 0x1 10102
1010 in decimal
23 22 21 20
8's 4's 2's 1's
5Base 8 (octal)
1768 1x64 7x8 6x1 1768 12610 in
decimal
82 81 80
64's 8's 1's
6Base 16 (hexadecimal)
- 2C
- The only thing different about hex is that we
need more characters than 0-9 to hold the16
possible numbers for each place. - So
- 10 becomes A
- 11 becomes B
- 12 becomes C
- 13 becomes D
- 14 becomes E
- 15 becomes F
2C16 2x16 12x1 2C16 4410 in decimal
161 160
16's 1's
7Converting Decimal to Binary
- I want to turn the number 1110 into binary
- Divide 11 by 2 5 Remainder 1
- Divide 5 by 2 2 Remainder 1
- Divide 2 by 2 1 Remainder 0
- Divide 1 by 2 0 Remainder 1
- ------------------------------------------------
- 1110 in binary (base 2) is 10112
8Converting Decimal to Hex
- I want to turn the number 5210 into binary
- Divide 52 by 16 3 Remainder 4
- Divide 3 by 16 0 Remainder 3
- ------------------------------------------------
- 5210 in hex (base 16) is 3416
9Going from Binary to Octal
- Going from binary to octal is a little easier.
- Each digit in an octal number (base 8) can be
represented by three binary (base 2) digits. - So to convert binary to octal, start with the 1's
place and group every three digits together to
form the new octal number10101010002 12508
0
5
2
1
10Going from Binary to Hex
- Similarly, each digit in an hex number (base 16)
can be represented by four binary (base 2)
digits. - So to convert binary to hex, start with the 1's
place and group every four digits together to
form the new hexadecimal digit10101010002
2A816
8
10
2
11Primitives/Objects in Java and Autoboxing
12Javas Primitive Data Types
- boolean (1 bit, true or false)
- byte (1 byte of storage, holding whole numbers
-128 to 127) - char (2 bytes of storage, holding 0 to 65,535,
corresponding to the Unicode character codes.
Digits 0 to 125 of Unicode correspond to ASCII) - short (2 bytes of storage, holding whole numbers
-32,768 to 32,767) - int (4 bytes of storage, holding whole numbers
-2,147,483,648 to 2,147,483,647) - float (4 bytes of storage, -3.4E38 to 3.4E38 w/6
to 7 digits of accuracy) - long (8 bytes of storage, holding whole numbers
-9,223,372,036,854,775,808 to 9,223,372,036,854,77
5,807) - double (8 bytes of storage, -1.7E308 to 1.7E308
w/14 to 15 significant digits)
13Primitive vs. Objects
- Recall the difference between primitives and
objects in Java primitives hold a value that is
just a value, Objects hold a value that can have
one or more instance variables and/or non-static
methods associated with it. - Also recall that autoboxing is Javas ability to
convert primitives to Objects and vice versa as
needed. - if you have a method that takes in an Integer
and you give it an int, Java will box or wrap
the int in a newly constructed Integer object
before passing it to the method - If you have a method that takes in an int, and
you give it an Integer, Java will automatically
unbox the Object and give the method its int
value
14Autoboxing/Unboxing Example
- The int value 5 is automatically converted to a
"new Integer(5)" when it is assigned to the
Integer object someNumberInteger someNumber 5 -
- The object someNumber is automatically "unboxed"
into a primitive when it is assigned to
anotherNumberInteger someNumber new
Integer(4) - int anotherNumber someNumber
15Generics and ArrayLists
- Recall that ArrayList is a class in Java defining
a dynamic data structure that can grow in size as
new elements are added - ArrayList uses Generics, which means the class is
defined using a generic type E that can be
applied to any Object class. - So in the API, the add method ispublic boolean
add(E o) - When you use an ArrayList, you replace E with
whatever type you are using when you constructed
the ArrayListArrayListltStringgt list new
ArrayListltStringgt() - list.add("Levi")list.add("Charles")
- String word list.remove(0)
16Generics and Autoboxing together
- ArrayListltIntegergt list new ArrayListltIntegergt()
- list.add(5) //5 is autoboxed and put
in the arraylist.add(new Integer(10))
//autoboxing not needed here - int num list.remove(0) //remove 1st
element, and auto-unbox - System.out.println(num) //5 prints out
17For-Each Loops
18A regular for loop
- String list "DOG", "CAT", "MOUSE"
- for(int i 0 iltlist.length i)
-
- System.out.println(listi)
-
19A For Each loop
- String list "DOG", "CAT", "MOUSE"
- for(String i list)
-
- System.out.println(i)
-
20For Each loop limitations
- While going through a collection with a for-each
loop, remember that you CANNOT modify, change,
remove, or replace any of the elements in the
collection. - So the following will compile and run, but WILL
NOT work as you would expect - String arr "dog", "cat", "mouse"
- for(String s arr)
- s s.toUpperCase() //won't affect original
array can't modify elements -
- System.out.println(Arrays.toString(arr)) //
dog, cat, mouse prints out
21printf
22C-style printf (formatted output)
- For us, this is really only useful for
competition-type situations where you need to get
formatted output quickly and easily, but it is
still kind of nice - In C, you can specify how you want your variables
to be printed out with (how many digits, how many
decimal digits, in scientific notation, in
decimal or in hex, etc) - This feature is finally in Java, using
System.out.printf()
23C-style printf (formatted output)
- String name "Chris"
- System.out.printf("Name 20s", name)
- System.out.println()
- System.out.printf("Name 5s", name)
- gives you
- Name Chris
- Name Chris
24- System.out.printf("String", input1,
input2...intputN) - Inside the first String argument, use the
following - argument_indexwidth.precisionconversion
- - signals that the next thing is going to come
from an input parameter following the String - argument_index - if you are going to use the
input parameters in the order they follow the
String (ie, input1, then input2, then input...)
you don't need this. Otherwise, put the argument
number followed by a to use that input
parameter...1 is the first input parameter, 2
is the 2nd - width the non-negative minimum number of spaces
the output should take up - .precision if talking about floats, the number
of decimal places to print out for the input
parameter. - conversion a single character showing what you
will be outputting the input parameter as...use - d for decimal numbers (ints)
- f for floating point numbers (doubles)
- o for integers in octal
- x for integers in hexadecimal
- s for Strings
- e for floating point numbers (doubles) in
Scientific Notation
25Lining up data with printf
- If we want a lined-up, formatted chart like this,
where the data will line up no matter what it is -
26- String item "PSP","Scooby Snacks","Paper
Clip","TV" - double price 199.95, .5, .0111, 300
-
- System.out.println(" Item Cost
") - System.out.println("-----------------------------
") -
- for(int i0 iltitem.length i)
-
- System.out.printf("17s 10.2f \n",
- itemi, pricei)
-
- System.out.println("-----------------------------
")
27Another fun thing print out a number in
scientific notation
Scientific notation is used to represent very
large or very small numbers. A number in
scientific notation has one number in front of
the decimal, the decimal, then the rest of the
number times 10 (or E, which stand for
"exponent") to some power. 1131 in scientific
notation is 1.131x103 0.000124 in scientific
notation is 1.24x10-4
- double num 1131.0
- double num2 -0.000124
- System.out.printf("21s\n", "scientific
notation") - System.out.println("---------------------")
- System.out.printf("21e\n", num)
- System.out.printf("21e\n", num2)
28What's the point of printf?
- Basically, this is just a way for you to make
sure your output is all pretty and lined up right
(so you can make pretty columns and get your
numbers lined up if you are printing something
out to the screen or something)
29Enums
30C style enums
- When Java first came out, if you had a set of,
say, three values you could assign to something,
you had to assign those values to either Strings
or integer constants - For example, in TicTacToe, each square could
either have an X, an O, or nothing. - So you might have made constant data fields
- public static final int NOTHING 0
- public static final int X 1
- public static final int Y 2
- if(blockObject.getContent() X) ...do
something
31C style enums
- Think about JOptionPane. It used
JOptionPane.ERROR_MESSAGE and a whole bunch of
other constants to specify the type message it
was. There are several problems with this. - 5.0 fixes this with Enumerations. They can be
fairly complex, but at the most basic level it is
like a "mini-class" that can only be given
certain values - It behaves very differently than C enumerations
(which are basically just ints), but can be used
similarly - public enum Block nothing, x, o
- Then
- if(blockObject.getObject() Block.nothing) do
something
32C style enums
- Enumerations can also be used in switch
statements - They are seen as safer than just using constant
ints, because they have a set number of values
they can be assigned (instead of using ints,
where you could assign any valid int and not get
a complaint from the compiler) - They will be very useful in next class's lab
33Static Imports
34Static import
- If you want to use a constant belonging to
another class (like Math.PI or Math.E), you have
to type the name of the class, the period, then
the name of the constant. - Same thing with methods...class name, period,
method name. - If you are doing a whole lot with the same class,
it can get redundant to keep typing the same
class name over and over
35Static imports
- If you use
- import static java.lang.Math.
- instead of the usual
- import java.lang.Math.
- at the top of your program, you will no longer
have to put the name of the class before each
constant or method. If you did it for the Math
class, you could then say - double circumference 2 PI 92.1
36Static imports
- I think this is stupid.
- It makes the code less readable...hard to
distinguish between your variables/methods and
ones you are getting from classes you import. - Its up to you if you want to use it or not.