Skip to main content

JAVA

 JAVA Cheat Sheet

Here's a cheat sheet summarizing some of the common concepts, syntax, and features of the Java programming language:

java
// Java Cheat Sheet // Hello World public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } } // Variables int age = 25; // Declare and initialize an integer variable double pi = 3.14; // Declare and initialize a double variable char letter = 'A'; // Declare and initialize a character variable boolean isTrue = true; // Declare and initialize a boolean variable String name = "John"; // Declare and initialize a string variable // Constants final int MAX_VALUE = 100; // Declare a constant variable // Data Types int // Integer double // Double precision floating-point number char // Character boolean // Boolean String // String (sequence of characters) // Operators +, -, *, /, % // Addition, Subtraction, Multiplication, Division, Modulus ++, -- // Increment, Decrement =, +=, -=, *=, /=, %= // Assignment // Control Flow if (condition) { // Code executed if condition is true } else if (condition2) { // Code executed if condition2 is true } else { // Code executed if no previous conditions are true } switch (variable) { case value1: // Code executed if variable is equal to value1 break; case value2: // Code executed if variable is equal to value2 break; default: // Code executed if no previous cases match } while (condition) { // Code executed repeatedly while condition is true } do { // Code executed at least once, then repeatedly while condition is true } while (condition); for (initialization; condition; increment/decrement) { // Code executed repeatedly until condition is false } // Arrays int[] numbers = {1, 2, 3}; // Array declaration and initialization int length = numbers.length; // Length of the array int element = numbers[index]; // Accessing an element // Classes and Objects class MyClass { private int x; // Private member variable public void setX(int value) { x = value; // Setter method } public int getX() { return x; // Getter method } } MyClass obj = new MyClass(); // Object instantiation obj.setX(5); // Accessing member functions int value = obj.getX(); // Inheritance class DerivedClass extends BaseClass { // Derived class definition } // Interfaces interface MyInterface { void myMethod(); // Method declaration } class MyClass implements MyInterface { public void myMethod() { // Method implementation } } // Exceptions try { // Code that may throw an exception } catch (ExceptionType e) { // Code to handle the exception } finally { // Code that always executes, regardless of whether an exception is thrown } // Input and Output import java.util.Scanner; // Import Scanner class Scanner scanner = new Scanner(System.in); // Create a Scanner object int number = scanner.nextInt(); // Read an integer input System.out.println("Hello, World!"); // Output to console // Packages import package_name.ClassName; // Import a specific class import package_name.*; // Import all classes in a package // Multithreading class MyThread extends Thread { public void run() { // Code executed in the thread } } MyThread thread = new MyThread(); // Create a thread object thread.start(); // Start the thread // Generics class MyClass<T> { T myMethod(T parameter) { // Method implementation return parameter; } } // File Handling import java.io.File; // Import File class import java.io.FileReader; // Import FileReader class import java.io.FileWriter; // Import FileWriter class import java.io.IOException; // Import IOException class File file = new File("filename.txt"); // Create a File object try { FileReader reader = new FileReader(file); // Create a FileReader object int character = reader.read(); // Read a character from the file reader.close(); // Close the reader FileWriter writer = new FileWriter(file); // Create a FileWriter object writer.write("Hello, World!"); // Write content to the file writer.close(); // Close the writer } catch (IOException e) { // Handle IOException } // JDBC (Java Database Connectivity) import java.sql.Connection; // Import Connection class import java.sql.DriverManager; // Import DriverManager class import java.sql.Statement; // Import Statement class import java.sql.ResultSet; // Import ResultSet class Connection connection = DriverManager.getConnection(url, username, password); // Establish a connection Statement statement = connection.createStatement(); // Create a Statement object ResultSet resultSet = statement.executeQuery("SELECT * FROM table"); // Execute a query while (resultSet.next()) { // Process the result set } connection.close(); // Close the connection // Java API Documentation // Official documentation: https://docs.oracle.com/en/java/javase/14/docs/api/index.html

This cheat sheet covers some of the commonly used concepts, syntax, and features of the Java programming language. It serves as a handy reference for quick look-ups and reminders while programming in Java.

Comments

Popular posts from this blog

Web Programming: HTML, DHTML, XML, Scripting, Java, Servlets, Applets

 Web programming encompasses various technologies and concepts used to develop web applications. Let's explore each of them in detail: 1. HTML (Hypertext Markup Language): HTML is the standard markup language used to create the structure and content of web pages. It uses tags to define elements like headings, paragraphs, images, links, forms, etc. Example: ```html <!DOCTYPE html> <html> <head>     <title>My Web Page</title> </head> <body>     <h1>Hello, World!</h1>     <p>This is a paragraph.</p>     <img src="image.jpg" alt="Image">     <a href="https://www.example.com">Visit Example</a> </body> </html> ``` 2. DHTML (Dynamic HTML): DHTML is a combination of HTML, CSS, and JavaScript that allows web pages to become more dynamic and interactive. Example (DHTML with JavaScript): ```html <!DOCTYPE html> <htm...

Variable Naming Rule

 When naming variables in the C programming language, it's important to follow certain rules and conventions to ensure clarity, readability, and maintainability of your code. Here are the general rules for variable naming in C: 1. Valid Characters:    - Variable names can consist of letters (both uppercase and lowercase), digits, and the underscore (_) character.    - The first character of a variable name must be a letter or an underscore. 2. Length Limitation:    - C does not impose a specific limit on the length of variable names, but it is recommended to keep them reasonably short and descriptive. 3. Case Sensitivity:    - C is a case-sensitive language, so variable names with different cases are treated as different variables.    - For example, "count" and "Count" are considered distinct variables. 4. Reserved Keywords:    - You cannot use C reserved keywords as variable names since they have predefined meanings in th...

Classes and Objects in python with Example

  # Classes and Objects class ClassName : def __init__ ( self, parameter1, parameter2 ): self.parameter1 = parameter1 self.parameter2 = parameter2 def method_name ( self ): # Code executed by the method object_name = ClassName(parameter1, parameter2) # Create an object object_name.method_name() # Access object's methods   In Python, classes and objects are the foundation of object-oriented programming (OOP). A class is a blueprint for creating objects, while an object is an instance of a class. Classes define attributes (variables) and methods (functions) that represent the characteristics and behaviors of objects. Let's explore some examples to understand classes and objects in Python. Example 1: Creating a simple class and object: ```python class Car: def __init__(self, make, model, year): self.make = make self.model = model self.year = year def display_info(self): print(f"Car: {self.year...