top of page

Variables

Variables are used in almost every coding language as well as mathematics. Variables are a symbol that represents numbers, letters or any other information. Like the name suggests variables are subject to vary and change. In Java, there exists two types of variables, those of primitive type and those of reference type. We use these variables to store information and data which we use in our code.

Capture.PNG

Data types in java

There are two types of variables in Java, those of primitive types and reference types.

Primitive types store the raw data in the variable, while reference types store an address of the data that is stored in memory.

 

Primitive types start with lowercase letters while reference types start with uppercase letters.

To declare and initialize a variable, you must first call a data type then follow it with the variable and initialize it to a value. Ex: int variableName = 3;

Using variables:To use a variable we just call the variable name to get the information stored in the variable. To store new information in a variable we simply initialize the variable to the new value.

int and string.PNG

Rules when using variables

A few rules while declaring variables:
1. the start of the first word of the variable name should be lowercase and the second starting with uppercase. We call this camelCase conventions

2. spaces don't work in a variable name, so if you need spaces use an underscore or a dash

3. variable names must be meaningful

4. putting only the data type and variable name is called declaring a variable, by setting the  variable to a value we are initializing the variable

5. your value of the variable must be compatible with the data type

6. Java is case sensitive so when calling the variable make sure you spelled it correctly

var rules.png

Variable types

image.png
image.png

The code snippet above shows different commonly used variable types

​

String: stores sentances and words, is a type of object and has methods

​

int: stores whole numbers

​

double: stores numbers that have decimals

​

Integer: an int in form of an object, same as int but has methods and is  treated as an object

​

Double: a double in form of an object, same as double but has methods and is treated as an object

​

char: stores a single character

Casting and parsing

image.png
image.png

The code snippet above shows what happens when dividing two integers. you get a float point error, which means that because they are both integers there cant be any decimals, when you divide 100 by 3 you get 33.333333 repeating. 

​

In the second print statement we cast 100 to a type double, which makes it output the right answer. To cast a variable to a different variable type we simply just put the type in brackets in front of the variable. 

This won't actually change the variable type, just change it for whatever purposes we need.

image.png
image.png

In the code snippet above, we a method of Integer and Double, which are the object equivalents of int and double. parseInt and parseDouble change type String into Integers and Doubles respectively

String methods

All objects have state and behavior, meaning that they have attributes and actions/methods they can execute. Since Strings are objects, they too have methods that can be called that are extremely useful. There are many String methods but the most important are shown in the code snippet below.
 

Learn more about Object methods in the methods tab here:

Pre-Req: String operate much like arrays (learn more about arrays here), where every letter is can be manipulated and is recognizable by an index.

For example: in the code snippet below there is our String variable with each index labelled, indexes always start from 0 and count up.

image.png
image.png
image.png
  1. str.charAt(int num):
    Returns the character of the String at the index given in the argument. In the example above it returns the character at index 0 which is "t"

     

  2. str.substring(int start, int end) OR str.substring(int start):
    2 parameters: returns a new string made of the letters between first index and last index, not including last index. In our case above it returns a string made of index 0,1 of str. which is "th"

    1 parameter: returns new string made of the letters after given index to end of the string. In our case above, it returns a string made of index 1 onward (so index 1,2,3) which is "his"
     

  3. str.indexOf(char c OR String word):
    Returns the index of the given char or String if found inside the String, if not in the String then it returns index -1. 

    As you can see when checking for str "s" it returns index 3, however when checking for index of str "p" it returns -1 since p is not in the word "this". 

    If there is more than one instance found within the String it will return the first instance. If you are searching for the index of a word within the String then it will return the index of the start of the first instance of the word.
     

  4. str.concat(String word): 
    Returns a new String made up of two Strings. The String passed in gets attached or concatenated to the end of the first String.

    str.equals(String word): is a method used to see if two Strings are equal in character, meaning they are the same word. The method returns true if they are equal and false if not.

    In this case we used .equals on the newString and passed in str.concat(word) which is what newString was instantiated to. returning true 
     

  5. str.compareTo(String word) and str.compareToIgnoreCase(String word):
    This method compares two Strings lexograhically, or in alphabetical order. The method returns an integer that represents how far apart on the english alphabet they are from each other. 

    However capitals are considered to be different from lowercase when comparing which is why the difference in .compareTo is 32 and the difference in compareToIgnoreCase() is only 8.

© 2024 by Samuel Wong.
Powered and secured by Wix

Call

(1) + 437-216-9455

Contact

@Brebeuf STEAM

  • Twitter
  • Instagram
bottom of page