-
Notifications
You must be signed in to change notification settings - Fork 0
/
java-programming.html
73 lines (67 loc) · 11.5 KB
/
java-programming.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<!DOCTYPE html SYSTEM "about:legacy-compat">
<html lang="en-US" data-preset="contrast" data-primary-color="#6860F6" data-link-color="#307FFF" data-resizable-sidebar="true" data-sidebar-width="260"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta charset="UTF-8"><meta name="built-on" content="2024-12-10T15:14:05.544512"><title>Java Programming | Computer Science Study Notes</title><script type="application/json" id="virtual-toc-data">[{"id":"Class-Terminology","level":0,"title":"1 Class Terminology","anchor":"#Class-Terminology"},{"id":"2-refernces-recursion-lists","level":0,"title":"2 References, Recursion, and Lists","anchor":"#2-refernces-recursion-lists"}]</script><script type="application/json" id="topic-shortcuts"></script><link href="https://resources.jetbrains.com/writerside/apidoc/6.10.0-b518/app.css" rel="stylesheet"><link href="static/custom.css" rel="stylesheet"><link rel="icon" type="image/png" sizes="16x16" href="Computer-Science-Study-Notes/photo.png"><meta name="image" content=""><!-- Open Graph --><meta property="og:title" content="Java Programming | Computer Science Study Notes"><meta property="og:description" content=""><meta property="og:image" content=""><meta property="og:site_name" content="Computer Science Study Notes Help"><meta property="og:type" content="website"><meta property="og:locale" content="en_US"><meta property="og:url" content="writerside-documentation/java-programming.html"><!-- End Open Graph --><!-- Twitter Card --><meta name="twitter:card" content="summary_large_image"><meta name="twitter:site" content=""><meta name="twitter:title" content="Java Programming | Computer Science Study Notes"><meta name="twitter:description" content=""><meta name="twitter:creator" content=""><meta name="twitter:image:src" content=""><!-- End Twitter Card --><!-- Schema.org WebPage --><script type="application/ld+json">{
"@context": "http://schema.org",
"@type": "WebPage",
"@id": "writerside-documentation/java-programming.html#webpage",
"url": "writerside-documentation/java-programming.html",
"name": "Java Programming | Computer Science Study Notes",
"description": "",
"image": "",
"inLanguage":"en-US"
}</script><!-- End Schema.org --><!-- Schema.org WebSite --><script type="application/ld+json">{
"@type": "WebSite",
"@id": "writerside-documentation/#website",
"url": "writerside-documentation/",
"name": "Computer Science Study Notes Help"
}</script><!-- End Schema.org --></head><body data-id="Java-Programming" data-main-title="Java Programming" data-article-props="{"seeAlsoStyle":"links"}" data-template="article" data-breadcrumbs=""><div class="wrapper"><main class="panel _main"><header class="panel__header"><div class="container"><h3>Computer Science Study Notes Help</h3><div class="panel-trigger"></div></div></header><section class="panel__content"><div class="container"><article class="article" data-shortcut-switcher="inactive"><h1 data-toc="Java-Programming" id="Java-Programming.topic">Java Programming</h1><section class="chapter"><h2 id="Class-Terminology" data-toc="Class-Terminology">1 Class Terminology</h2><p id="-chakjd_5">In Java, classes can contain not just functions (a.k.a. methods), but also data.</p><p id="-chakjd_6">To run a class, we must define a main method (but not all classes have a main method). Unlike python, there's no need to import if the two files are in the same project.</p><p id="-chakjd_7">Classes can be instantiated as objects. The class provides a blueprint that all instances (objects) will follow.</p><p id="-chakjd_8">A variable or method defined in a class is also called a member of that class.</p><aside class="prompt" data-type="note" data-title="" id="-chakjd_9"><p id="-chakjd_16">It's better to know object-oriented programming here. Check out <a href="python-programming.html#6-object-oriented-programming" id="-chakjd_17" data-tooltip="OOP in Python">OOP in Python</a> for basic understanding!</p></aside><p id="-chakjd_10"><span id="-chakjd_18"><font style="color:#cd5c5c">Example with Terminology</font></span></p><div class="code-collapse" data-lang="java" data-is-expanded="false" data-synopsis="/* penguin.java */">
/* penguin.java */
public class penguin {
public int age; // Instance variables, can have as many of these as you want
public static String binomen = "Spheniscidae"; // Static variable, shared by all instances of the class
/* Constructor (similar to a method, but not a method), determines how to instantiate the class */
public penguin(int age) {
this.age = age;
}
/* Static Method, can be called without creating an instance of the class */
public static void makeNoise() {
System.out.println("Aw!");
}
/* Non-static method, a.k.a. Instance Method, can only be called by creating an instance of the class */
public void addage(int years) {
age += years;
}
public static penguin compareAge(penguin p1, penguin p2) {
if (p1.age > p2.age) {
System.out.println("p1 is older");
return p1;
} else {
System.out.println("p2 is older");
return p2;
}
}
}
</div><div class="code-collapse" data-lang="java" data-is-expanded="false" data-synopsis="/* Main.java */">
/* Main.java */
public class Main {
public static void main(String[] args) {
penguin p1 = new penguin(5); // Instantiation and Assignment, ok to separate them
p1.makeNoise(); // Aw! (Not Recommended, but works)
p1.addage(2); // Calling non-static method
System.out.println(p1.age); // 7
penguin p2 = new penguin(3);
penguin older = penguin.compareAge(p1, p2); // p1 is older, return p1
// Static methods must access instance variables via a specific instance!
penguin.makeNoise(); // Aw! (Calling static method)
penguin.addage(2); // Error, cannot call non-static method without an instance
penguin.name("Penguin"); // Syntax Error!
}
}
</div><aside class="prompt" data-type="tip" data-title="" id="-chakjd_13"><p id="-chakjd_19"><span id="-chakjd_21"><font style="color:#8a2be2">Key differences between static and non-static (a.k.a. instance) members:</font></span></p><ul class="list _bullet" id="-chakjd_20"><li class="list__item" id="-chakjd_22"><p id="-chakjd_24">Static memberss are invoked using the class name, e.g. <code class="code" id="-chakjd_27">penguin.makeNoise();</code>, <code class="code" id="-chakjd_28">penguin.binomen</code></p><p id="-chakjd_25">It is acceptible to use instance to invoke static methods (e.g. <code class="code" id="-chakjd_29">p1.makeNoise();</code>), but some compilers and IDEs will issue a warning.</p><p id="-chakjd_26">Static methods must access instance variables via a specific instance (<code class="code" id="-chakjd_30">penguin.compareAge(p1, p2)</code>)!</p></li><li class="list__item" id="-chakjd_23"><p id="-chakjd_31">Non-static members must be invoked using instances, and cannot be invoked using class name!</p></li></ul></aside><p id="-chakjd_14"><span id="-chakjd_32"><font style="color:#8a2be2">Initialing an array</font></span></p><div class="code-comparer" id="-chakjd_15" data-comparing="vertically"><div class="code-block" data-lang="java" data-title="Java">
int[] arr1 = new int[5]; // Array of 5 elements, all initialized to 0
int[] arr2 = {1, 2, 3, 4, 5};
</div><div class="code-block" data-lang="python" data-title="Python">
arr1 = [0] * 5
arr2 = [1, 2, 3, 4, 5]
</div></div></section><section class="chapter"><h2 id="2-refernces-recursion-lists" data-toc="2-refernces-recursion-lists">2 References, Recursion, and Lists</h2><p id="-chakjd_35">8 primitive types in Java: <code class="code" id="-chakjd_45">byte</code>, <code class="code" id="-chakjd_46">short</code>, <code class="code" id="-chakjd_47">int</code>, <code class="code" id="-chakjd_48">long</code>, <code class="code" id="-chakjd_49">float</code>, <code class="code" id="-chakjd_50">double</code>, <code class="code" id="-chakjd_51">boolean</code>, <code class="code" id="-chakjd_52">char</code>.</p><p id="-chakjd_36">Everything else, including arrays, is a reference type.</p><p id="-chakjd_37">The <code class="code" id="-chakjd_53">new</code> keyword allocates memory on the heap, a region of memory dedicated to dynamic object allocation. This memory space holds the instance variables (data members) of your new object.</p><p id="-chakjd_38">The constructor of the class is then invoked. The constructor's job is to initialize the object's state by setting the initial values of the instance variables.</p><p id="-chakjd_39">Once the constructor completes, new returns a reference (address) to the newly created object.</p><p id="-chakjd_40"><span id="-chakjd_54"><font style="color:#8a2be2">The Golden Rule of Equals (and Parameter Passing):</font></span> Given variables b and a, b = a copies all the bits from a into b, and passing parameters obeys the same rule.</p><aside class="prompt" data-type="note" data-title="" id="-chakjd_41"><p id="-chakjd_55">There may be a little confusion here: When you pass a primitive type (e.g., <code class="code" id="-chakjd_57">int</code>, <code class="code" id="-chakjd_58">float</code>, <code class="code" id="-chakjd_59">boolean</code>, etc.) to a method, a copy of the value is made and passed to the method. Modifying the parameter within the method does not affect the original variable.</p><p id="-chakjd_56">When you pass an object to a method, a copy of the reference to the object is passed. The reference itself is passed by value, but the reference points to the original object on the heap. This means when you modify the object within the method, the changes are reflected in the original object.</p></aside><p id="-chakjd_42"><span id="-chakjd_60"><font style="color:#8a2be2">Arrays in Java</font></span></p><div class="code-block" data-lang="java">
int[] a = new int[]{0, 1, 2, 95, 4}; // Declaration & Instantiation
</div><section class="procedure-steps"><h3 id="-chakjd_44" data-toc="-chakjd_44">Declaraing & Instantiating Arrays</h3><ol class="list _decimal"><li class="list__item" id="-chakjd_61"><p id="-chakjd_64">Creates a 64 bit box (reference) for storing an int array address. (declaration)</p></li><li class="list__item" id="-chakjd_62"><p id="-chakjd_65">Creates a new Object, in this case an int array. (instantiation)</p></li><li class="list__item" id="-chakjd_63"><p id="-chakjd_66">Puts the address of this new Object into the 64 bit box named a. (assignment)</p></li></ol></section></section><div class="last-modified">Last modified: 10 December 2024</div><div data-feedback-placeholder="true"></div><div class="navigation-links _bottom"><a href="database-system.html" class="navigation-links__prev">Database System</a><a href="operating-system.html" class="navigation-links__next">Operating System</a></div></article><div id="disqus_thread"></div></div></section></main></div><script src="https://resources.jetbrains.com/writerside/apidoc/6.10.0-b518/app.js"></script></body></html>