Data types are arguably the best solid foundation you can start with when you first step into the world of Java. There are different data types like primitive and relative types. It is the main idea of how to store or manipulate data in Java and how it could be referenced. A variable in java has to be a specific data type.
A single digit number variable that can be manipulated or added to other int variables.
A number variable that has a decimal point that can be manipulated or added to other double variables.
Used to hold unicode charcters, like 'a' or '@'.
The long variable is a variable that can store whole numbers at a larger storage range (64 bit) and a longer number range from -9223372036854775808 and a 9,223,372,036,854,775,807.
The long variable is a variable that can store whole numbers at a small storage range (64 bit) and a longer number range from -32768 and 32767.
The float variable can store fractional numbers from 3.4e−038 to 3.4e+038. All values have to end with a "f".
The byte variable is a variable that can store whole numbers at a larger storage range (8 bit) and a very short number range of -128 to 127.
A variable that has two outcomes, true or false. This can be for the manipulated in methods, loops, etc.
String = 16 bits, 2 Bytes per character
Int = 32 bits, 4 Bytes
Double = 64 bits, 8 Bytes
Char = 16 bits, 2 Bytes
Boolean = 8 bits, 1 byte
This is a data type that is very simple as it holds the data you put directly in the variable. It’s the actual value, so when manipulated or changed the actual value is also changed as well. They represent singular things, no methods or complex structures are included, just the raw value.
Also known as non-primitive data types, it contains addreses/references to an object that is created by YOU, the programmer (except String). It doesn't store the data itself, but a address/reference to the memory location where its stored.
These methods below are the String methods, which are methods that can manipulate Strings to solve many problems or further innovate code.
This uses the KEY EXAMPLE by printing it.
HOW TO CALL: strName.substring(a,b)
Substring is one of the main methods used in Java Strings. it allows you to access, modify, print or manipulate a specific part of a String. Substring starts at indexes 0, not 1. For example in the word “Brebeuf”, the B is at index 0, not 1. The first parameter is always inclusive, whilst the second is always exclusive. Variable a is where it starts and variable b is where it ends.
KEY EXAMPLE: To get the substring “Bre” from Brebeuf which is initialized in the variable str1, the code would be “str1.substring(0, 3);”.
This uses the KEY EXAMPLE by adding it to a new String, and printing it.
This is an screenshot of the KEY example.
HOW TO CALL: strName.toUpperCase() and strName.toLowerCase()
This method converts a string regardless of case into its upper or lowercase form.
REMEMBER, when you convert it doesn't actually convert the original string, the best way to counterract is to store the command in a variable to keep the conversion.
KEY EXAMPLE:
IF str1 = “dog”, str1.toUpperCase() = DOG
IF str2 = “DOG”, str2.toLowerCase() = dog
This is an screenshot of the KEY example.
HOW TO CALL: strName.length()
This method will find out the length of a string starting from 1.
KEY EXAMPLE:
"dog" will return a length of 3 when the method is called.
This is an screenshot of the KEY example.
HOW TO CALL: strName.equals(otherStrName)
This method will compare two Strings and return true if they are equal and false if they are not.
KEY EXAMPLE:
IF str1 = dog, str2 = dog
str1.equals(str2) = true
IF str1 = dog, str2 = cat
str1.equals(str2) = false
This is an screenshot of the KEY example.
This is an screenshot of the KEY example.
HOW TO CALL: strName.equalsIgnoreCase(otherStrName)
the equalsIgnoreCase() method is very helpful as it will only look at the word itself, ignoring if its uppercase or lowercase.
KEY EXAMPLE:
"IF str1 = dog, str2 = DOG
str1.equalsIgnoreCase(str2) = true
IF str1 = dog, str2 = CAT
str1.equalsIgnoreCase(str2) = false
This is an screenshot of the KEY example.
This is an screenshot of the KEY example.
HOW TO CALL: charAt(a)
This method will return the string at the specified index. charAt() ALSO starts at indexes 0, not 1. For example in the word “Gerudo”, the G is at index 0, not 1. In variable a you should add the index pof what part of string you want to use.
KEY EXAMPLE:
IF I want to get the letter "y" from the string "myth" (named str1), I would call str1.charAt(1);.
This is if I wanted the letter "m" instead of "f".
This is an screenshot of the KEY example.
HOW TO CALL: strName.indexOf(String a) or strName.indexOf(String a, int b)
This method will return the postition of the first occurence of a specific string or individual character at the specified index. This is shown in the first way to call it where variable a is where you put the string you want to find the first occurence of in your String variable.
We can take this a step further by doing the same thing, but adding another parameter which is the variable b at the top. This variable tells the method where to start from. So if you want to find the letter "a" from "dada", and start at position 2, it will skip the first a.
SAME RULES APPLY: INDEXES/POSITIONS START AT 0, NOT 1.
KEY EXAMPLE:
IF I want to find the first occurence of the letter "y" from the string "mythy" (named str1), I would call str1.indexOf("y");
This is if I wanted to start searching the key example at position/index 2.
This is an screenshot of the KEY example.
HOW TO CALL: strName.lastIndexOf(String a)
This method will return the postition of the last occurence of a specific string or individual charcter at the specified index. This is shown in the first way to call it where variable a is where you put the string you want to find themlast occurence of in your String variable.
SAME RULES APPLY: INDEXES/POSITIONS START AT 0, NOT 1.
KEY EXAMPLE:
IF I want to find the first occurence of the letter "y" from the string "mythythy" (named str1), I would call str1.lastIndexOf("y");