Skip to content

Java Basics

Sharina Stubbs edited this page Sep 17, 2019 · 11 revisions

Resources

Class Sample Code: https://github.com/codefellows/seattle-java-401d6/blob/master/class-01/demo/java-fundamentals-401d6/basics/Basics.java

Under the hood:

  • There will be much that is not understood... yet... but knowing how to use it is more important initially.
  • Java is not a scripting language. It isn't built with the goal of running a small bit of code.

Imports

  • In IntelliJ imports happen automatically
  • When not using IntelliJ, ie, the terminal and VS Code, you have to be attentive to imports
  • Must import even things built into Java other than the absolute basics.

Creating a Java File:

  • file name matches exactly with class name.

Setting up a class in the file

  • public class FileNameExactly
  • Running a file means running the main method. The main method always looks exactly like this: public static void main(String[] args) { }
  • Compiling can be done on the command line, with a javac + file_name. If there's no output, that's a good sign compiling went well, otherwise the alternative is an error. $ javac Basics.java
  • run the code in the terminal after compiling by typing in $ java Basics <-- it just needs to know to run java Basics. Hint: Use tab to autocomplete when running the code.

Writing a method

  • For now, they always start with "public static"
  • In a method declaration, you need to include a return type, as in, what type of data is being returned from this method... or void.
  • Then, need the name of the method, like isItSalmonSeason()
  • Check if it should take in parameters, but anytime there are any variable whatsoever, you have to specify what type of data type, so a parameter looks like methodName(String month)
  • If you write another method besides the main method, you have to call the second method within the primary method, since running a file means running the main method.

Equality

  • Double equals (==) is checking if something is exactly the same string instance, including the location within memory. Double equals is fine for primitive data types, but not so otherwise. Testing same instances of a string class. Note that primitives are lowercase. if(month == "September") { return true;
  • Instead, .equals works well for objects (everything besides primitives). Testing if two things are of the same piece of data.
  • Note that you cannot use if month; without being specific, because Java is all about specificity.
Clone this wiki locally