JavaBasics1

Java Basics 1.13

Class Variables

  • Java has no CONSTANT declaration so......

// public - accessible from entire app

// static - class var

// final - value can't be changed

IN Olive()...

public static final long BLACK= 0x000000;

using it

public long color = Olive.BLACK;

 

 

Java Basics 1.12

Instance Methods

  • Class method called from definition of the class
    • building up utility functions that pass all data in the call
    • STATIC present
  • Instance method call from instance of the class - OBJECT
    • objects stick around and retain their data so its always accessible.
    • STATIC missing
  • Method declarations
    • static - class method
    • public - called anywher ein ap
    • private - only within class
    • protected - only

Java Basics 1.11

Encapsulation

 

  • Encapsulate functions by wrapping sections of code in a class/method
  • Now reuseable
  • Changing in one place, not many

 

Code:

 

 

Java Basics 1.10

Simple Arrays

 

Arrays NOT resizeable at runtime..

 

Code:

 

public class Main {

   

    public static void main(String[] args) {

 

      int[] a1 = new int[3]; //array of 3 integers

      for (int i = 0; i < a1.length; i++) {

            System.out.println(a1[i]);

      }

      

      int a2[] = new int[3]; //array of 3 integers

Java Basics 1.09

Error Handling

Code:

public class Main {

 

    public static void main(String[] args) {

      

      //declarations

      //String s;...need to assign value

      String s = null;

      System.out.println(s);

      

      //Arrays

      String[] strings = {"Welcome!"}; //one item in array

Java Basics 1.08

Using Equality Operators

 

 

Code:

public class Main {

 

    public static void main(String[] args) {

      String s1 = ("Welcome to California");

      String s2 = new String("Welcome to California");

      System.out.println(s2);

      

      if (s1 == s2) { //comparing OBJECTS not VALUES

            System.out.println("With == They match");

Java Basics 1.07

Passing By Copy; Primitives

When calling a function, a COPY of the argument is passed to the method

     void incrementValue(int inFunction) {

            inFunction ++;

            System.out.println("In function: " + inFunction);

      }

      

      int original = 10;

      System.out.println("Original before : " + original);

      incrementValue(original);

      System.out.println("Original after : " + original);

 

Java Basics 1.06

Methods

 

Refactoring

 

 

Code:

 

public class Main {

 

   public static void main(String[] args) {

      doSomething();

      //refactoring, copy code, Refactor..

      //will create a new method and reference it here

      loopMe();

Java Basics 1.05

Switch Statements: Integers

Code:

import java.io.BufferedReader;

import java.io.InputStreamReader;

 

public class SwitchWithInts {

 

   public static void main(String[] args) {

      String input = getInput("Enter a number between 1 and 12: ");

      int month = Integer.parseInt(input);

      

      switch (month) {

      case 1:

Java Basics 1.04

Characters

Code:

     public static void main(String[] args) {

      char c1 = '1';

      char c2 = '2';

      char c3 = '3';

 

      //unicode

      char dollar = '\u0024';

      

      System.out.print(dollar);

      System.out.print(c1);

      System.out.print(c2);

Subscribe to JavaBasics1