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

Tokens, Identifiers, Data Types, Sequence Control, Subprogram Control, Arrays, Structures, Union, String, Pointers, Functions, File Handling, Command Line Argumaents, Preprocessors in C with example

 Let's discuss each concept briefly and provide examples for better understanding: 1. Tokens: Tokens are the smallest building blocks in C programming. They include keywords, identifiers, constants, strings, operators, and punctuators. Example: ```c #include <stdio.h> int main() {     int num = 42;  // 'int', 'main', 'return', '42', '=', ';' are tokens     printf("Hello, World!");  // 'printf', '(', ')', 'Hello, World!', ';', are tokens     return 0;  // 'return', '0', ';' are tokens } ``` 2. Identifiers: Identifiers are names used to identify variables, functions, or other user-defined entities. Example: ```c int age = 30;  // 'age' is an identifier (variable name) void displayMessage() {  // 'displayMessage' is an identifier (function name)     // function body } ``` 3. Data Types: Data types define the type of data that can be stored in ...

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...

Place holder and control character in c language

 In the C programming language, placeholders and control characters are used to format and control the output of text in console-based programs. They are special characters or sequences of characters that have specific meanings. Here are the placeholders and control characters commonly used in C: 1. Placeholders:    - %d: Used to display signed integers.      Example: printf("The value is %d", 10);    - %u: Used to display unsigned integers.      Example: printf("The value is %u", 10);    - %f: Used to display floating-point numbers.      Example: printf("The value is %f", 3.14);    - %c: Used to display characters.      Example: printf("The character is %c", 'A');    - %s: Used to display strings (sequence of characters).      Example: printf("The string is %s", "Hello");    - %p: Used to display memory addresses (pointers)...