Using the Java Library — Chapter 6

Java library/Java API

The Java Standard Edition (Java SE) ships with hundreds of pre-built & compiled classes.

Classes are grouped into packages

In the case of not importing import java.util.ArrayList; , you have to use the entire package name in order to use a class java.util.ArrayList list = new java.util.ArrayList0 ;.

java.lang —(only class that is) imported by default (pre-imported). System.out.println(), Math.random() belong to this.

javax.swing — holds some of the Swing GUI classes

java.util — holds a pile of utility classes

java.net

java.io

java.awt

  1. Helps the overall organization group code into packages for specific kinds of functionality
  2. Give you a name scoping, to help prevent collisions.
  3. Provides a level of security, using restrictions.
  • It’s a class in the core Java A
  • An array that shrinks when you remove something.
  • Gets values out without knowing their index
  • Dynamically changes size
  • To add primitives to an ArrayList you need to wrap them in primitive wrapper class (then use unwrapping to take it out when wanted)(this happens automatically).
  • Should import java.util.ArrayList; to use
  • Accepts any Object (ultimate polymorphic type)

Few methods from ArrayList

  • add(Object e)
  • remove(int index) — returns true if element was in the list
  • remove(Object e)
  • contains(Object e) — returns true if contains
  • isEmpty() — returns true if empty
  • indexOf(Object e)-returns -1 if not found
  • size()
  • get(int index)

ArrayList<Person> personList=new ArrayList<Person>(); //empty

ArrayList vs Array

  • Size is needed for initiation
  • Specific index assignment is needed (might overflow at runtime if max length is reached)
  • Special unique syntax
  • Grows and shrinks accordingly
  • Can either add at an index or from the behind
  • Common object syntax
  • Are parameterized (Should mention the type of parameter ArrayList<String> or ArrayList<Dog>(can hold either that type or subclasses of that type))

Boolean Expressions

Also known as short circuit operators, the main boolean expressions are ‘AND’ and ‘OR’ Operators (&&, ||)

If the JVM sees that the left side of && expression is false, it doesn’t look at the right.

If the JVM sees that the left side is true, it declares the entire statement to be true and doesn’t bother with the right side.

This behaviour of the JVM could be used in cases such as this,

A reference variable that is either assigned or not assigned to an object, is used to call a method. If it is null (no object is assigned), you’ll get a NullPointerException. So instead you can,

if (refVar != null && refVar.isvalidType() ){

//do ‘got a valid type’ stuff

}

This first checks if refVar is null and then only precedes with the .isValidType(). (This way JVM skips the .isValidType() if is null part returned false)

Non Short Circuit Operators (&, I )

They act similar to && and || but, checks both sides of the boolean expression.

Is typically used in the context, of manipulating bits.

Note

import statements does not make the class any bigger at all. It’s simply a matter of coding convenience.

Refer to Java API (Java docs) for more information.

Thanks for reading. Until next time! 👋🏽

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Upulie Handalage

Upulie Handalage

Everything in my point of view. Here for you to read on....