Skip to main content

Java For Python Programmers Edition 2

Section 3.8 Summary & Reading Questions

  1. All variables must be declared with their type before use in Java due to static typing.
  2. Java has primitive types (int, float, double, char, boolean) and their corresponding Object wrapper classes (Integer, Float, Double, Character, Boolean) with automatic conversion between them called autoboxing.
  3. The import statement in Java allows you to use shortened class names instead of fully qualified names like java.util.Scanner.
  4. Scanner objects are used to read input from various sources, including System.in for keyboard input.
  5. Java Strings are immutable like Python, but use methods like charAt() and substring() instead of indexing operators.
  6. ArrayList is Java’s dynamic array implementation that is similar to Python lists, but must declare the type of objects it contains using generics like ArrayList<Integer>.
  7. Maps (HashMap and TreeMap) are Java’s equivalent to Python dictionaries for storing key-value pairs.

Reading Questions Reading Questions

1.

What is the correct way to declare an ArrayList that will hold String objects in Java?
  • ArrayList<String> list;
  • Good job!
  • ArrayList list;
  • No, you must specify the type of objects it will hold using generics.
  • ArrayList[String] list;
  • No, the syntax for generics uses < > brackets, not [ ].
  • ArrayList() list;
  • No, this is not a valid declaration.

2.

The process of automatically converting between primitive types and their Object wrapper classes in Java is called:
  • Autoboxing
  • Right! Good job!
  • Casting
  • No, casting is manually converting a type.
  • Overloading
  • No, overloading refers to methods, not type conversion.
  • Unboxing
  • No, unboxing is the reverse of autoboxing.

3.

Which method would you use to get the character at position 3 in a Java String called str?
  • str.charAt(3)
  • Correct!
  • str[3]
  • No, Java does not support this type of indexing.
  • str.get(3)
  • No, this method does not exist in Java.
  • str.substring(3)
  • No, this will return a substring starting at position 3, not a single character.
You have attempted of activities on this page.