SNCA:Java

From Soyjak Wiki, the free ensoyclopedia
Jump to navigationJump to search
Why yes, Java is a gigachad.
How could you tell?
Java mascot, Duke, as an jak.

Java, also known as Jahwa is a general purpose programming language used in numerous applications, such as game development. It is used on Netflix and Google[1][2], on devices such as Android phones and Smart TVs, and on your PC for RuneScape and Minecraft. It is object oriented, and forces everything (even the program itself) to be an object. It is a very gemmy language due to its massive standard library, making it convenient to use. You probably also used it for playing Java ME[a] games on your dad's cell phone. Did you know? Java runs on your credit card.[3]

Tutorial[edit | edit source]

Java Basics
Menu
Basics
In Java, the file name must match the name of the public class. For example, if your class is public class NusoiProgram, save the file as NusoiProgram.java. This is required for the program to compile.
Boilerplate
Every simple Java program needs this structure. The program always starts with a public class and a main() method. The main() method is where the program begins execution.
public class NusoiProgram {
    public static void main(String[] args) {
        // Your code goes here
    }
}
Hello World
Here's a simple hello world app. The println() method prints text and moves to the next line automatically.
public class NusoiProgram {
    public static void main(String[] args) {
        System.out.println("Hello Saar!");
    }
}
Variables
Java uses variables to store data. Each variable has a type, like <def title="Integer">int</def> for numbers, double for decimals, and String for text. You WILL say what type of variable it is before setting it doe.
public class NusoiProgram {
    public static void main(String[] args) {
        int age = 25;
        double price = 2000.31;
        String name = "Bajeet";
        System.out.println(name + " is " + age + " years old and the price of his laptop was " + price + " rupees.");
    }
}
Adding Numbers
You can read numbers from the user and add them together. import java.util.Scanner; adds the Scanner class from the package java.util into the code. The Scanner class allows you to get input from the console.
import java.util.Scanner;

public class NusoiProgram {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter first number: ");
        int firstNumber = scanner.nextInt();

        System.out.print("Enter second number: ");
        int secondNumber = scanner.nextInt();

        System.out.println(firstNumber + " + " + secondNumber + " = " + (firstNumber + secondNumber));
    } 
}
Convert time
This is almost exactly like the previous example, but using multiplication instead. Notice how we are using print() instead of println() to get user input- this is to make the cursor be on the same line as the question.
import java.util.Scanner;

public class NusoiProgram {

    public static void main(String[] args) {
        Scanner nusoi = new Scanner(System.in);
        System.out.print("Enter time in minutes: ");
        
        int minutes = nusoi.nextInt();
        int seconds = minutes * 60;
        
        System.out.println(minutes + " minutes is " + seconds + " seconds.");
    }
}
Printf
We will introduce printf(), which is a version of print() that accepts C-style format specifiers. %d formats numeric types and %s formats strings. %n is a newline specifier.
public class NusoiProgram {
    public static void main(String[] args) {
        int age = 25;
        double price = 2000.31;
        String name = "Pajet";
        System.out.printf("%s is %d years old and the price of his laptop was %d rupees.%n", name, age, price);
    }
}
Star pyramid
A star pyramid is a pyramid of stars. WAAAOOOOW! This script below generates it.
import java.util.Scanner;

public class NusoiProgram {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the number of rows: ");
        int numOfRows = input.nextInt();
        for (int i = numOfRows; i > 0; i -= 2){
            for (int j = 1; j <= i / 2; ++j) {
                System.out.print(" ");
            }
            for (int k = i; k <= numOfRows; ++k) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
Packages and modules
A package is essentially a grouping of classes. Each file may have at most one package declaration, and it must be before any other statements in the file. A package name typically consists of of the structure TLD, then organisation name, then subdirectories (e.g. package com.mysoft.tools for the tools package of a company whose website is mysoft.com, or package party.soyjak.soy; for soyjak.party/soy). Each package is named after its location in the app's file system structure, so for example the package party.soyjak.soy would reside in (from the project root): ./src/main/java/party/soyjak/soy. (Note that the names of packages begin after ./src/main/java.)

Packages in Java do not have "sub-packages", thus java.util.concurrent is not a "sub-package" of java.util. Such names are only used to indicate that the package is associated.

package party.soyjak.examples;

public class NusoiProgram {
    public static void main(String[] args) {
        System.out.printf("%s is %d years old and the price of his laptop was %d rupees.%n", "Pajeet", 25, 2000.31);
    }
}

After this, one can import classes from defined packages, like so:

import party.soyjak.booru.BooruNamefag;
import party.soyjak.util.captcha.McChallenge;

Or, to import all classes from the package, use a wildcard import:

import party.soyjak.util.*;

As mentioned earlier, this will only import packages directly in party.soyjak.util, but not in "sub-packages" like party.soyjak.util.captcha.

In Java 9 and later, packages can be even further grouped together using modules, which can declare the dependencies (using requires) and exported packages (using exports) of each module. However, these are only particularly useful in large applications and libraries where encapsulation must be clearly defined. However, entire modules can be imported, such as import module java.base; which imports a large subsection of the Java standard library.

module party.soyjak.tools {
    requires java.sql;

    exports party.soyjak.tools;
    exports party.soyjak.tools.utils;
}
Classes, interfaces, inheritance
A class should have a constructor (which has no return type and bears the same name as the class), which is used with the keyword new to instantiate a new instance. Each Java source file can have at most one public declaration. You can make a class inherit another class's features by using a extends clause. In Java, it is only possible to extend a single class. All classes implicitly extend Object. However, you can inherit as many interfaces as you would like; an interface is essentially a set of methods. When overriding a function, indicate it is overriden with the @Override annotation.
package party.soyjak.examples;

// Base class of vehicles, which implicitly extends Object
class Vehicle {
    private String brand;
    private int year;

    public Vehicle(String brand, int year) {
        this.brand = brand;
        this.year = year;
    }

    public void displayInfo() {
        System.out.printf("Brand: %s, Year: %d%n", brand, year);
    }
}

// Interface for flying vehicles
interface Flyable {
    void fly();
}

// Interface for drivable vehicles
interface Drivable {
    void drive();
}

// Car class extends Vehicle and implements Drivable
class Car extends Vehicle implements Drivable {
    private int doors;

    public Car(String brand, int year, int doors) {
        super(brand, year); // calling Vehicle constructor
        this.doors = doors;
    }

    @Override
    public void drive() {
        System.out.printf("Driving the car with %d doors.%n", doors);
    }

    public void displayCarInfo() {
        displayInfo();
        System.out.printf("Doors: %s%n", doors);
    }
}

// Airplane class extends Vehicle and implements Flyable
class Airplane extends Vehicle implements Flyable {
    private int maxAltitude;

    public Airplane(String brand, int year, int maxAltitude) {
        super(brand, year); // calling Vehicle constructor
        this.maxAltitude = maxAltitude;
    }

    @Override
    public void fly() {
        System.out.printf("Flying the airplane at max altitude of %d metres.%n", maxAltitude);
    }

    public void displayAirplaneInfo() {
        displayInfo();
        System.out.printf("Max Altitude: %d metres.%n", maxAltitude);
    }
}

public class NusoiProgram {
    public static void main(String[] args) {
        Car car = new Car("Toyota", 2022, 4);
        Airplane airplane = new Airplane("Boeing", 2020, 35000);

        car.displayCarInfo(); // Display car details
        car.drive(); // Driving the car

        System.out.println();

        airplane.displayAirplaneInfo(); // Display airplane details
        airplane.fly(); // Flying the airplane
    }
}
GUI
This uses Swing. The code snippet below is used in this applet, but can also be used in normal desktop apps. Remember, NetBeans makes Java GUIs drag and drop, just like Visual Basic.
package party.soyjak.examples;

import java.awt.event.ActionEvent;

public class NusoiProgram {
    private void jButton1ActionPerformed(ActionEvent evt) {
         int num1 = (int) jSpinner1.getValue();
         int num2 = (int) jSpinner2.getValue();
         int calculation = num1 + num2;
         jTextField1.setText(Integer.toString(calculation));
    }
}

This uses AWT. Remember, NetBeans makes Java GUIs drag and drop.

package party.soyjak.examples;

import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;

public class NusoiProgram extends JFrame {
    public HelloWorldApp() {
        setTitle("Helo saar");
        setSize(300, 200);
        setVisible(true);
    }

    @Override
    public void paint(Graphics g) {
        g.drawString("Hello saar", 100, 100);
    }

    public static void main(String[] args) {
        HelloWorldApp instance = new HelloWorldApp();
        instance.paint(new Graphics2D());
    }
}
Next Steps
Here are some resources to continue learning Java:

Extra notes for beginners:

  • public static void main(String[] args) is the entry point of your program. Only one method with this signature is allowed. The JVM looks for a method with this exact signature to execute the program.
  • Print methods:
    • print() – prints text without moving to a new line.
    • println() – prints text and moves to a new line.
    • printf() – prints text with format specifiers (like in C), no newline automatically.
  • The java.lang package is always available; you don't need to import it. This package contains important globally relevant classes like String, Math, and System.
  • A public class can be used by other programs outside its package.
Final Step to Mastery

More about Java[edit | edit source]

WARNING: WORDSWORDSWORDS
This page or section is just one big wall of text.

It is based on C++, but without all the pain of pointers and other hassles. It has always been quite popular in America, with Python only overtaking it in recent years.[4] Sadly, it is also associated with 'jeets because a lot of jeets learn Java from Durgasoft. Linus Torvalds, the creator of Linux, called it an "awful language", though xe is a tranny and xis opinions don't matter.

Despite the similar name, it is not related to JavaScript (a web scripting language that many pajeets learn because it is a braindead language for retards).

Unlike C++, which is compiled directly to machine code tied to an operating system, Java is compiled into bytecode interpreted by the JVM—an intermediate virtual machine enabling platform independence, dynamic class loading, easy decompilation, and powerful runtime reflection capabilities.

One of the main boasts of Java is its "write once, run anywhere" slogan, which basically means that due to the JVM which runs on top of the operating system, there is no need to recompile code: you just have to have the .jar file and run that. Everything just runs on the JVM, so Java code is essentially platform independent. Also, Java is basically dialect-free. Most people use OpenJDK, which is an open-source development of the Java Development Kit (JDK). There are many build systems available for Java, the most common are Maven and Gradle (you should probably just use Gradle since it is simple and modern and less verbose than Maven o algo).

Java is a very 'emmy language for general use, such as making any application. You can also make 'cord bots with Java, which can be useful if you want to make a bot to spam or troll 'cord 'rannies, though you could also do this with Python. There are several popular libraries such as JDA for this. However, because of Java's high memory footprint due to its massive runtime and garbage collection (with almost no manual memory/resource management), it is less suitable for making games or operating systems with.

Unlike C and C++, Java is garbage collected which means you can just allocate memory and not have to manually deallocate it. So Java is memory safe (like Rust fellow trans queens).

A long time ago you could use Java to make applets which were like mini programs you could run in your browser. But Jews took this away from us because muh (((security))) and now we only have JeetScript. But Flash will always be a gem, and Flash ActionScript is heavily based on ECMAScript with Java-style syntax and types.

In C and C++, dereferencing a null object crashes the program due to a segmentation fault. However, in Java, dereferencing/accessing a null object throws a NullPointerException.

Example[edit | edit source]

package party.soyjak.example;

import java.util.Random;

/**
 * A simple program to decide whether to ack.
 * Java 21 or higher required due to `Math.clamp` method.
 */
public class Tranny {
    private static final Random rand = new Random();

    /**
     * Determines whether the tranny should ack based on a given probability.
     *
     * @param ackProbability An integer between 0 and 100 representing the chance of acking.
     * @return True if the random probability is less than the ackProbability; otherwise, False.
     */
    private static boolean shouldAck(int ackProbability) {
        double prob = rand.nextDouble() * 100;
        return prob < Math.clamp(ackProbability, 0, 100);
    }

    /**
     * Main function that decides whether to ack or scream TRANS RIGHTS ARE HUMAN RIGHTS!!!!!!!! based on probability.
     *
     * @param args An array of command line arguments.
     * @return Whether the program executed successfully.
     */
    public static void main(String[] args) {
        if (shouldAck(41)) {
            throw new RuntimeException("ACK!!!!");
        } else {
            System.out.println("TRANS RIGHTS ARE HUMAN RIGHTS!!!!!!!!");
        }
    }
}

Java applets[edit | edit source]

Screenshot of an applet running.

Java applets were miniature apps written in the Java programming language that could run in the browser by a sandboxed JVM. They were used for interactive charts, 3d games, richer ui, soyence simulations, banking, and file upload / auto refreshing things. They were quickly abandoned because they could access the user's entire computer due to the constant sandbox escaping vulnerabilities, made worse if the user had the plugin run automatically.

They were replaced with Flash quickly because you can do almost everything in Flash way more easily. There was a small push to add applet support to soyjak.st, but quickly died out because it was silly. Java still is a good programming language so don't let this deter you from using it. If you want to use Java applets securely use jRunner - no plugins o algo.

Sample applet[edit | edit source]

You can test a simple calculator applet (using cheerpj js applet runner) here. Also have a look at the below applet. This applet is a horizontal navigation bar with rollover and drop-down effects. The buttons don't actually link to anything but that is possible.

package party.soyjak.example;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JApplet;

public class SunMenuApplet extends JApplet implements MouseListener, MouseMotionListener {
    private static final int MENU_ITEM_WIDTH = 100;
    private static final int MENU_ITEM_HEIGHT = 30;
    private static final int SUB_ITEM_HEIGHT = 20;

    private final String[] menuItems = {"Imageboard", "Wiki", "Booru"};
    private final String[][] subItems = {
        {"Index", "/soy/", "rules", "pass", "bans"},
        {"Recent Changes", "Watchlist", "Notifications"},
        {"The Dailyjak", "Your shit posts"}
    };

    private int hoverMenu = -1;
    private int hoverSub = -1;

    private int getMenuIndex(int x) {
        return (x >= 0 && x < menuItems.length * MENU_ITEM_WIDTH) ? x / MENU_ITEM_WIDTH : -1;
    }

    private int getSubIndex(int y) {
        if (hoverMenu == -1) {
            return -1;
        }

        int subY = y - MENU_ITEM_HEIGHT;
        return (subY >= 0 && subY < subItems[hoverMenu].length * SUB_ITEM_HEIGHT) ? subY / SUB_ITEM_HEIGHT : -1;
    }

    @Override
    public void init() {
        addMouseListener(this);
        addMouseMotionListener(this);
    }

    @Override
    public void paint(Graphics g) {
        g.setColor(Color.LIGHT_GRAY);
        g.fillRect(0, 0, size().width, MENU_ITEM_HEIGHT);
=
        for (int i = 0; i < menuItems.length; i++) {
            int x = i * MENU_ITEM_WIDTH;

            if (i == hoverMenu) {
                g.setColor(Color.GRAY);
                g.fillRect(x, 0, MENU_ITEM_WIDTH, MENU_ITEM_HEIGHT);
            }

            g.setColor(Color.BLACK);
            g.drawRect(x, 0, MENU_ITEM_WIDTH, MENU_ITEM_HEIGHT);
            g.drawString(menuItems[i], x + 10, SUB_ITEM_HEIGHT);
        }

        if (hoverMenu != -1) {
            int menuX = hoverMenu * MENU_ITEM_WIDTH;
            int menuY = MENU_ITEM_HEIGHT;
            int subHeight = subItems[hoverMenu].length * SUB_ITEM_HEIGHT;

            g.setColor(Color.WHITE);
            g.fillRect(menuX, menuY, MENU_ITEM_WIDTH, subHeight);
            g.setColor(Color.BLACK);
            g.drawRect(menuX, menuY, MENU_ITEM_WIDTH, subHeight);

            for (int j = 0; j < subItems[hoverMenu].length; j++) {
                int y = menuY + j * SUB_ITEM_HEIGHT;

                if (j == hoverSub) {
                    g.setColor(Color.LIGHT_GRAY);
                    g.fillRect(menuX + 1, y + 1, MENU_ITEM_WIDTH - 2, SUB_ITEM_HEIGHT - 2);
                    g.setColor(Color.BLACK);
                }

                g.drawRect(menuX, y, MENU_ITEM_WIDTH, SUB_ITEM_HEIGHT);
                g.drawString(subItems[hoverMenu][j], menuX + 5, y + 15);
            }
        }
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        int mx = e.getX();
        int my = e.getY();
        int newHoverMenu = getMenuIndex(mx);
        int newHoverSub = getSubIndex(my);

        if (newHoverMenu != hoverMenu || newHoverSub != hoverSub) {
            hoverMenu = newHoverMenu;
            hoverSub = newHoverSub;
            repaint();
        }
    }

    @Override
    public void mouseExited(MouseEvent e) {
        hoverMenu = -1;
        hoverSub = -1;
        repaint();
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (hoverMenu != -1 && hoverSub != -1) {
            System.out.println("Clicked: " + subItems[hoverMenu][hoverSub]);
        }
    }

    // leave these blank because Dr. Soyberg told you so
    @Override
    public void mouseDragged(MouseEvent e) {
        // Left unimplemented
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // Left unimplemented
    }

    @Override
    public void mousePressed(MouseEvent e) {
        // Left unimplemented
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // Left unimplemented
    }
}

The applet packages were located in java.applet and also java.awt, with extended capabilities in javax.swing. Applets were deprecated in Java 9 even though nobody uses Java 9 or above.[b]

Java applets were proposed for addition to soyjak.party but were not supported (unlike Flash) due to potential security risks.[c]

Gallery[edit | edit source]

Citations

Notes

  1. Micro Edition, a smaller version of Java for your cell
  2. because Oracle wanted developers to bundle the JRE with each application instead because people got confused when some .jar files didn't execute due to being libraries, unlike .exe and .dll being seperate. Thoughbeit Java 9 is quite gemmy as it introduces the Java Platform Module System which allows you to explicitly control what packages are public, and in Java 25 you can directly import modules.
  3. someone getting your cookies and sending it off to HTTPbin o algo
IRC

Java is part of a series on Computer Science.

Languages Low Level AssemblyCC++C#Holy CRust

High Level JavaGoPHPPythonSQLBashJavaScriptPowerShellActionScriptScratchRubyLua

Markup HTMLCSSSVGXML
Software Imageboards nusoiVichanYotsubaOpenYotsuba

OSes WindowsLinuxAndroidTempleOS

Other BabybotMcChallengeSystemdMS PaintJS PaintPhotoshopFlash
AI

ChatGPTGeminiGrokVibe codingGenerative AIStable Diffusion

More SoyGNUCGIDDoSGame developmentPiracyRegexDoxingMicrosoftAppleGoogleDataminingWebPArtificial soyduelRatio duelingCustomizationRicingFSLWindows debloating