-
Notifications
You must be signed in to change notification settings - Fork 2
Java Introduction
Ella Noyes edited this page Aug 16, 2023
·
3 revisions
-
Boolean: two possible values: true or false
Boolean myBool = true;
-
int: whole number
int num1 = 23;
-
float: fractional number, ends in f
float num2 = 3.4f;
-
String: a sequence of characters
String password = “e3$op10f!”;
-
Other Java data types include: double, long, short, char, array,...
-
You can also have custom data types:
- If you want to create a program that implements custom blocks in Minecraft, you need to define what that block is in terms that a computer will understand
- Is this block related to other structures, e.g. a simple Minecraft library block like stone?
- What sort of information defines that block, e.g. name, strength, tool required for drops, light emitted?
- What can a block do?
- Structures can be broken back down into primitive data types
- Describe the scope of variables and methods
- The first keyword in declaring a variable or method
- Sensitive or irrelevant information should not accessed outside of a class/file
- Access specifiers [1], in order of most accessible to most restrictive:
-
public
(class, package, subclass, global) -
protected
(class, package, subclass) - Default–when no modifier is given (class, package)
-
private
(class)
-
-
final
keyword: Once declared as final, the variable cannot be changed/reassigned
- Type of conditional statement
- If a condition is met, then perform a task
- Define a condition using:
- Comparison operators: <, >, <=, >=, ==, !=
- Logical operators: ! (not), && (and), || (or)
- Logical expression returns true or false
- E.g., Make an announcement if an integer x equals 10
if(x == 10){
System.out.println("Your number is 10!");
}
- Carry out a task
- A method is “called”
- Six components:
- Access specifier (first keyword of declaration)
- Return type (data type i.e., String, Boolean, int, … or void if nothing is returned)
- Name (just before parentheses)
- Parameters (in parentheses)
- Body (in the curly brackets)
- Return statement (unless method has void return type)
- A basic method:
public static int sum(int num1, int num2){
int num3 = num1+num2;
return num3;
}
public static <T> void func1(T input){
System.out.println("Your input is of "+input.getClass());
}
- More complex method:
public static <B extends Block> RegistryObject <Item> fromBlock(RegistryObject<B> block) {
return ITEMS.register(block.getId().getPath(), () -> new BlockItem(block.get(), new Item.Properties()));
}
- Questions to ask yourself when looking at a method:
- What is the method name?
- Access specifiers?
- What is the return type?
- Is a method being called in the method body?
- Do instances, methods, or variables of other classes appear in the method body?
- Class: structure that contains a set of specific methods and variables
- E.g., An Item is defined by variables (maxCount, maxDamage, fireproof, etc.), and methods (Boolean damage(...), ActionResult useOnBlock(...), Boolean isFood(), etc.). Different items will have different variables and method implementations, but are all related to this class (or structure), known as Item
- Have a constructor, a method used to create an object of that class
-
static
keyword: A static method or variable is its own entity, it can be accessed without instantiating the class - Inheritance: Classes that extend existing classes
- E.g., ItemFood extends Item (or ItemFood is-a(n) Item)
- It inherits variables and methods of Item class, and has its own additional set of characteristics (like methods getHealAmount(...), or getMaxItemUseDuration(...))
- E.g., ItemFood extends Item (or ItemFood is-a(n) Item)
- Logic is similar to generic methods
- Must be instantiated with a type parameter
- Minecraft modding example:
public static final RegistryObject<Block> SPECIAL_MUSHROOM_BLOCK = BLOCKS.register("special_mushroom_block", ()->new Block(BlockBehaviour.Properties.of(Material.STONE).strength(4f).requiresCorrectToolForDrops()));
An object of class RegistryObject, with type Block.
- Many files required in a mod, keeping those files organised is crucial–allows the programmer and collaborators to find specific information/code
- Packages group together related files and provide structure
- Remain consistent in your packaging
- A basic skeleton is provided in the Forge documentation
- Look at other popular mods and how they implement packaging
- A functional interface is an interface with exactly one abstract method [2] (method that has been declared but not implemented)
- We can instantiate the interface and implement its abstract method–this is the essence of a lambda expression
- Three components:
- Parameters/arguments (in parentheses)
- Arrow (->) connects parameter and method body
- Method body *A lambda expression:
NotScaredOfLambdas nsol = () -> System.out.println("I'm not scared of lambdas");
- Web tutorials:
- w3schools: https://www.w3schools.com/java
- GeeksforGeeks: https://www.geeksforgeeks.org/java
- Dev.java: https://dev.java (also see video resources)
- Other:
- Browse GitHub source code for beginner projects
- Create a simple project for yourself, learn concepts as they come up, explore your questions on https://stackoverflow.com/
- Concordia resources:
- Library stacks
- Check out Udemy courses
- Kanopy: see The Great Courses
[1] GeeksforGeeks, "Access Modifiers in Java," geeksforgeeks.org. https://www.geeksforgeeks.org/access-modifiers-java (accessed Jun. 13, 2023).
[2] Oracle, "Writing Your First Lambda Expression," dev.java. https://https://dev.java/learn/lambdas/first-lambdas/ (accessed Jun. 13, 2023).