RSC

Please login or register.

Login with username, password and session length
Advanced search  

News:

Join Our Discord: https://discord.gg/ESkVEnq

Author Topic: Rodgerwilcos Intro to Java Part 2: Primitive Data Types  (Read 122 times)

Rodgerwilco

  • Administrator
  • Steel
  • *****
  • Posts: 9
    • View Profile
Rodgerwilcos Intro to Java Part 2: Primitive Data Types
« on: August 01, 2017, 07:30:42 AM »

Information about primitive data types can be found here: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

The goal of this tutorial is to show you how to use them.

If we were to create an object named Player and wanted to store data about that Player, you would use primitive data types. The type is chosen by memory limitations. An unsigned (non negative) byte for example has a maximum value of 255. Short 65535, Integer 2,147,483,647 and Long 9,223,372,036,854,775,807. In the nuts and bolts tutorial provided you will notice there are more than the four I have mentioned, feel free to use them!
 
When you're designing/brainstorming the contents of an object, you're going to want to figure out what variables your object will have. In our scenario we have a Player and it's up to your imagination to implement whatever the hell you want. For this example I will be implementing the use of 4 variables: kills, deaths, money and a boolean skulled.

Code: (java) [Select]
public class Player{

       private short kills; //(we might have more than 255 kills but no more than 65,535)
       private short deaths;
       private int money;
       private boolean skulled;

       //noargs constructor
       public Player(){

       }
}

We are not required to set the value of our variables because data types have default values. However we do have two options on how we want to assign a value to the variables. We can either create a constructor with assigned values such as:

Code: (java) [Select]
       public Player(short kills, short deaths, int money, boolean skulled){
              this.kills = kills;
              this.deaths = deaths;
              this.money = money;
              this.skulled = skulled;
       }

or we can use a setter to set the value after the object has been created. For example:

Code: (java) [Select]
public class Player{

       private short kills; //(we might have more than 255 kills but no more than 65,535)
       private short deaths;
       private int money;
       private boolean skulled;

       //noargs constructor
       public Player(){

       }

       public void setKills(Short amount){
             this.kills = amount;
       }

       public void setDeaths(Short amount){
             this.deaths = amount;
       }

       public void setMoney(int amount){
              this.money = amount;
       }

       public void setSkulled(boolean skulled){
              this.skulled = skulled;
       }
}

Testing the code above would look something like this:

Code: (java) [Select]
public static void main(String[] args) {
Player p = new Player();
                p.setKills(5);
                p.setDeaths(3);
                p.setMoney(25000);
                p.setSkulled(true);
}
Logged