CS 61B Part 0 Basic Java
next:[[61B_Part_1_Java]]
Java program skeleton
no need to worry about the retract(缩进) (like in python).
Just like C++!
1 | public class ClassNameHere { |
conditions
if
1 | if (x < 10) |
other version:
1 | if (x < 10) |
else & else if
1 | if (dogSize >= 50) { |
loops
while
1 | int bottles = 99; |
for
1 | for (int i = 0; i < a.length; i++) { |
another version of the for loop in java:
1 | public class EnhancedForBreakDemo { |
of course, you can use break; or continue; in Java
user-defined functions
use public static before making a user-defined functions
All parameters, and functions themselves master have a type.
All functions must be in a class, so all functions in Java are methods.
1 | public static int max(int x, int y) { |
data types
Important ones:
In Java, varibles are first varified before the code runs!!!
If there are type issues, Java would simply not run.
(You don’t want your TikTok broken down when having a video)
normal ones
int, double, string
Arrays (list in Python)
1 | int[] numbers = new int[3]; |
simpler one:
1 | int[] numbers = new int[]{4, 7, 10}; |
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
