Introfuction to Java
Introfuction to Java
Hello world:
● Java has a Java Virtual Machine, which ensures the same
Java code can be run on different operating systems and
platforms. Sun Microsystems’ slogan for Java was “write
once, run everywhere”.
● Java files have a .java extension. Files make up a program.
● Inside HelloWorld.java, we had a class:
○ public class HelloWorld {
}
○ Syntax inside of the curly braces ia a part of the class
○ Each file has one primary class named after the file. Our class name is
HelloWorld and our file name is HelloWorld.
● Inside the class, we had a main() method which lists our program tasks:
○ public static void main(String [] args) {
}
○ (public, static, and void are syntax we’ll learn later on) String [] args is a
placeholder for information we want to pass into our program. This syntax is
necessary for the program to run.
Java Strings:
● Strings are used for storing text
● A String variable contains a collection of characters
surrounded by double quotes:
○ String greeting = “Hello”;
● A String in Java is an object, which contains methods that
can perform certain operations on strings. For example,
the length of a string can be found with the length ()
method:
○ String text =
“ABCDEFGHIJKLMNOPQRSTUVWXYZ”
System.out.println(“The length of the text string is: ” + text.length());
Methods:
● Print something: System.out.print(“Hello World);
● Print something then make a new line: System.out.println(“Hello World);
○ System is a built-in Java class that contains useful tools for our programs
○ out is short for output
○ println is short for print line
● Find the length of a string: length()
○ String text = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”
System.out.println(“The length of the text string is: ” + text.length());
● Convert a string to all capital letters: toUpperCase()
○ String text = “Hello World”;
System.out.println(text.toUpperCase()); //Outputs “HELLO WORLD”
● Convert a string to all lowercase letters: toLowerCase()
○ String text = “Hello World”;
System.out.println(text.toLowerCase()); //Outputs “hello world”
● Returns the index (position) of the first occurrence of a specific text in a string
(including whitespace): indexOf()
○ String text = “Please locate where ‘locate’ occurs!”;
System.out.println(text.indexOf(“locate”)); //Outputs 7
Java counts positions from zero. So, 1 is the first position in a string, 1 is the second, 2 is the
third… etcetera
● Returns the character at the specified index (position) in a string (including
whitespace): charAt()
○ String text = “I like donuts!”;
System.out.println(text.charAt(“o”)); //Outputs 9
● Compares two strings lexicography: compareTo()
○ String myStr1 = "Hello";
String myStr2 = "Hello";
System.out.println(myStr1.compareTo(myStr2)); //Returns 0 because they are equal
○ The comparison is based on the Unicode value of each character in the strings.
○ The method returns 0 if the string is equal to the other string. A value less than
0 is returned if the string is less than the other string (less characters) and a
value greater than 0 if the string is greater than the other string (more
characters).
● Compares two strings lexicography, ignoring lowercase and uppercase decisions:
compareToIgnoreCase()
○ String myStr1 = "HELLO";
String myStr2 = "hello";
System.out.println(myStr1.compareToIgnoreCase(myStr2)); //Returns 0 because they are equal
○ The compareToIgnoreCase() method compares two strings lexicographically,
ignoring lowercase and uppercase differences.
○ The comparison is based on the Unicode value of each character in the string
converted to lowercase.
○ The method returns 0 if the string is equal to the other string, ignoring case
differences. A value less than 0 is returned if the string is less than the other
string (less characters) and a value greater than 0 if the string is greater than the
other string (more characters).
● Compares two strings to see if they are equal: equals()
○ String myStr1 = "Hello";
String myStr2 = "Hello";
String myStr3 = "Another String";
System.out.println(myStr1.equals(myStr2)); // Returns true because they are equal
System.out.println(myStr1.equals(myStr3)); // false
○ The equals() method compares two strings and returns true if the strings are
equal, and false if they are not
● Finds out if a string contains a sequence of characters: contains()
○ String myStr = “Hello”;
System.out.println(myStr.contains(“Hel”)); //true
System.out.println(myStr.contains(“o”)); //true
System.out.println(myStr.contains(“Hi”)); //false
○ Returns true if the characters exist, and false if not
● Concatenate two strings: concat()
○ String firstName = “John”;
String lastName = “Doe”;
System.out.println(firstName.concat(lastName)); //John Doe
○ The concat() method appends (concatenate) a string to the end of another string.
● Returns a new string where all x characters are replaced with y characters: replace()
○ String myStr = “Hello”;
System.out.println(myStr.replace(‘l’, ‘p’); //Heppo
○ The replace() method searches a string for a specified character, and returns a
new string where the specified character(s) are replaced.
○
Vocabulary:
● Classes - Classes are a collection of data and the actions that can modify the data.
Programming is a very abstract task. Classes were created to give users a mental model
of how to think about data in a more concrete way. Classes act as the blueprint. They
tell Java what data is collected and how it can be modified.
● Objects - Objects are constructed according to the blueprint that is the class. In the
code above, the variable s is a string object. It is not the class. The string class tells Java
that s has methods like length, concat, and replace. When a programmer wants to use a
class, they create an object.
● Instance - Another way that programmers talk about objects is to say that an object is
an instance of a particular class. For example, s is an instance of the String class.
● Instantiation - Instantiation is the process where an object is created according to
blueprint of the class. The phrase “define a variable” means to create a variable. The
variable is given a name and a value. Once it has been defined, you can use the variable.
With objects, you use the phrase "instantiate an object". That means to create an object,
give it a name, store any data, and define the actions the object can perform.
● The constructor is a special method for a class. Its job is to assign value
for attributes associated with the object. These attributes can also be
called instance variables. In Java, the constructor is the class name,
parentheses, and curly brackets. Inside of the constructor, give
attributes their values.
●
● Up until now, the attributes created in the class are independent from
one another when new objects are created. These are called object
attributes. You can create an instance of the Actor class for Helen
Mirren and another for Dwayne Johnson. Each object has different
values for their attributes. A class attribute, however, is an attribute
whose value is shared by each instance of a class.
● To illustrate this, add the attribute static String union to the Actor
class. The static keyword tells Java that this attribute is a class
attribute. Set the value of union to the string "Screen Actors Guid".
Notice that class attributes are declared and initialized with a value
outside of the constructor.
● A deep copy is when Java makes a copy of the object and its attributes.
● A shallow copy is when Java makes a copy of the object, but the attributes are shared.