I'm very new to Java; and have only self learned the basic java class. Now I'm studying the inheritance part; here is the sample code I got from online.
I have my code on my sublime, then I compile and run in my mac terminal.
I believe the error occurs when I load my package
Here is my code:
package types;
public class Types {
public static void main(String[] args) {
// == Types ==
// Every variable has a type. Types fall into two categories.
// (1) Primitive types
// - Primitive values are NOT objects.
// - The value (not a reference to it) sits directly in the box.
// - byte, short, int, long, float, double, boolean, char
// - Primitive type names begin with lowercase letters.
// Declare that age is a variable and its type is int; assign it
// the value 5.
// The variable does not contain a reference to an object with value 5;
// the variable contains 5 itself.
int age = 5;
boolean lovesPrincesses = true;
double shoeSize = 12.5;
char initial = 'C';
// We can't redeclare a variable.
// int age = 8;
// But we can assign a new value to it.
age = 8;
// We can't assign it anything other than int values.
// age = 8.7;
// age = true;
// (2) Class types
// - the variable contains a reference to an object.
// - We must explicitly construct each object using "new"
// Declare that s1 is a variable whose type is String; construct a new
// object of type String and store a reference to it in s1.
String s1 = new String("hello");
// == Wrapper classes and autoboxing ==
// Every primitive type has a wrapper class version. It can be used to
// represent a primitive value when an Object is needed.
Integer i2 = new Integer(5);
Boolean b2 = new Boolean(false);
System.out.println(i2);
System.out.println(b2);
// Java can automatically "box" a primitive into an instance of its
// wrapper class.
Integer x = 6; // automatically does Integer i = new Integer(6)
// And it can automatically "unbox" a wrapper object to get the
// primitive value.
int y = x + 4; // automatically does int y = x.intValue() + 4;
// == Strings ==
// Strings are objects.
// Strings are immutable.
// You can construct a String explicitly, but Java allows a shortcut:
// you can omit the "new".
String s = new String("hello");
String s2 = "bye";
// Because Strings are immutable, this actually constructs a brand new
// String rather than appending to the old one.
s2 = s + s2;
System.out.println(s2);
// Indexing
char letter = s2.charAt(3); // Python: s2[3]
System.out.println(letter);
// Slicing
String slice = s2.substring(4); // Python: s2[4:]
System.out.println(slice);
slice = s2.substring(5, 7); // Python: s2[5:7]
System.out.println(slice);
// Stripping (remove whitespace from beginning and end of String.)
String s3 = " hi there ";
s3 = s3.trim();
System.out.println(s3);
// Splitting.
s3 = "before hi there after";
String[] parts = s3.split("e");
System.out.println(parts[0]);
System.out.println(parts[1]);
System.out.println(parts[2]);
// Out of bounds:
// System.out.println(parts[3]);
// == Arrays ==
// Not like Python lists!:
// - fixed length, which is set when constructing the object
// - all elements must have the same type
int[] intArray = new int[4];
System.out.println(intArray[0]);
String[] stringArray = new String[20];
System.out.println(stringArray[0]);
stringArray[0] = "blunderbuss";
System.out.println(stringArray[0]);
intArray = new int[] { 1, 2, 3 };
System.out.println(intArray[1]);
}
}
Here is what happen when I run it on my temrinal
Last login: Wed Dec 28 05:38:20 on ttys000
LaitekiMacBook-Pro:~ Lai$ cd Documents
LaitekiMacBook-Pro:Documents Lai$ cd workspace
LaitekiMacBook-Pro:workspace Lai$ ls
Types.java sample_test.java
LaitekiMacBook-Pro:workspace Lai$ javac Types.java
LaitekiMacBook-Pro:workspace Lai$ java Types
Error: Could not find or load main class Types
LaitekiMacBook-Pro:workspace Lai$ java types.Types
Error: Could not find or load main class types.Types
LaitekiMacBook-Pro:workspace Lai$
Both of my Types.class and Types.java are now saved in my workspace folder; and this is my working directory as well
Can someone explain this in more detail (not merely the solution but why is this happen and how to fix it)