{"id":18176984,"url":"https://github.com/sahilrajput03/learning_java","last_synced_at":"2025-06-14T10:33:34.522Z","repository":{"id":109133390,"uuid":"439969268","full_name":"sahilrajput03/learning_java","owner":"sahilrajput03","description":null,"archived":false,"fork":false,"pushed_at":"2024-11-18T12:30:50.000Z","size":98,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-07T10:53:54.049Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sahilrajput03.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-12-19T21:14:09.000Z","updated_at":"2024-11-18T12:30:54.000Z","dependencies_parsed_at":"2024-12-20T22:42:12.789Z","dependency_job_id":"51da7605-6734-4781-8dd9-1984a6924d2c","html_url":"https://github.com/sahilrajput03/learning_java","commit_stats":{"total_commits":24,"total_committers":1,"mean_commits":24.0,"dds":0.0,"last_synced_commit":"8bfea5854f795dd0068e1f11905c6c86e3e0cfa5"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/sahilrajput03/learning_java","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sahilrajput03%2Flearning_java","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sahilrajput03%2Flearning_java/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sahilrajput03%2Flearning_java/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sahilrajput03%2Flearning_java/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sahilrajput03","download_url":"https://codeload.github.com/sahilrajput03/learning_java/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sahilrajput03%2Flearning_java/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259802345,"owners_count":22913616,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-02T17:12:00.696Z","updated_at":"2025-06-14T10:33:34.503Z","avatar_url":"https://github.com/sahilrajput03.png","language":"Java","readme":"# Rockstart - Java\n\n**Quick Links:**\n- javaMonTesting: [github.com/sahilrajput03/javaMonTesting](https://github.com/sahilrajput03/javaMonTesting)\n\n## Contents:\n\n- **_[with spring and live reloading](spring-live-java/README.md)_**\n\n---\n\nLearning Java continute from [here](https://www.w3schools.com/java/java_modifiers.asp)\n\n## good friend - constructor\n\nNote that the constructor name must match the class name, and it cannot have a return type (like void).\n\nAlso note that the constructor is called when the object is created.\n\nAll classes have constructors by default: if you do not create a class constructor yourself, Java creates one for you. However, then you are not able to set initial values for object attributes.\n\n```java\n// Create a Main class\npublic class Main {\n  int x;  // Create a class attribute\n\n  // Create a class constructor for the Main class\n  public Main() {\n    x = 5;  // Set the initial value for the class attribute x\n  }\n\n  public static void main(String[] args) {\n    Main myObj = new Main(); // Create an object of class Main (This will call the constructor)\n    System.out.println(myObj.x); // Print the value of x\n  }\n}\n\n// Outputs 5\n```\n\n## Making use of variables of class members\n\n```java\n// Modify Attributes\npublic class Main {\n  int x;\n\n  public static void main(String[] args) {\n    Main myObj = new Main();\n    myObj.x = 40;\n    System.out.println(myObj.x);\n  }\n}\n```\n\n- If you create multiple objects of one class, you can change the attribute values in one object, without affecting the attribute values in the other:\n\n```java\npublic class Main {\n  int x = 5;\n\n  public static void main(String[] args) {\n    Main myObj1 = new Main();  // Object 1\n    Main myObj2 = new Main();  // Object 2\n    myObj2.x = 25;\n    System.out.println(myObj1.x);  // Outputs 5\n    System.out.println(myObj2.x);  // Outputs 25\n  }\n}\n```\n\n## Making objects of a class\n\nsrc: https://www.w3schools.com/java/java_classes.asp\n\n(^^ do read if you wanna dig inside dust..)\n\n**FYI** :In the end of above source link(w3school) you'll learn how difference files can access eachother's class members to use them. Yikes! (similar to modules in nodejs but java sucks!)\n\n```java\npublic class Main {\n  int x = 5;\n\n  public static void main(String[] args) {\n    Main myObj = new Main();\n    System.out.println(myObj.x);\n  }\n}\n```\n\n## Java convention ?\n\nRemember from the Java Syntax chapter that a class should always start with an uppercase first letter, and that the name of the java file should match the class name.\n\n## Loading code from another file in java\n\nSrc: https://stackoverflow.com/a/43350661/10012446\n\nTake a look:\n\n• Non-static method:\n\n```java\n// B.java\n\nclass B {\n   public void myMethod() {\n     // do stuff\n   }\n}\n\n\n// A.java\n\nclass A {\n    public void anotherMethod()\n    {\n         B b=new B();\n         b.myMethod();        // calling B class's method\n    }\n}\n```\n\n• STATIC method:\n\n```java\n// B.java\n\nclass B {\n   public static void myMethod() {\n     // do stuff\n   }\n}\n\n\n// A.java\nclass A {\n    public void anotherMethod()\n    {\n         B.myMethod();        // calling B class's method\n    }\n}\n```\n\n## recursion practise in java\n\n```java\npublic class Main {\n  public static void main(String[] args) {\n    int result = sum(10);\n    System.out.println(result);\n  }\n  public static int sum(int k) {\n    if (k \u003e 0) {\n      return k + sum(k - 1);\n    } else {\n      return 0;\n    }\n  }\n}\n```\n\nHalting Condition\nJust as loops can run into the problem of infinite looping, recursive functions can run into the problem of infinite recursion. Infinite recursion is when the function never stops calling itself. Every recursive function should have a halting condition, which is the condition where the function stops calling itself. In the previous example, the halting condition is when the parameter k becomes 0.\n\nIt is helpful to see a variety of different examples to better understand the concept. In this example, the function adds a range of numbers between a start and an end. The halting condition for this recursive function is when end is not greater than start:\n\n```java\npublic class Main {\n  public static void main(String[] args) {\n    int result = sum(5, 10);\n    System.out.println(result);\n  }\n  public static int sum(int start, int end) {\n    if (end \u003e start) {\n      return end + sum(start, end - 1);\n    } else {\n      return end;\n    }\n  }\n}\n```\n\n## method overloading\n\n```java\nstatic int plusMethodInt(int x, int y) {\n  return x + y;\n}\n\nstatic double plusMethodDouble(double x, double y) {\n  return x + y;\n}\npublic static void main(String[] args) {\n  int myNum1 = plusMethodInt(8, 5);\n  double myNum2 = plusMethodDouble(4.3, 6.26);\n  System.out.println(\"int: \" + myNum1);\n  System.out.println(\"double: \" + myNum2);\n}\n```\n\n## lovely methods\n\nMethods are used to perform certain actions, and they are also known as functions. Why use methods? To reuse code: define the code once, and use it many times.\n\n```java\npublic class Main {\n  static void myMethod() {\n    // code to be executed\n  }\n\n  static void myMethod2(String fname, int age) {\n    System.out.println(fname + \" is \" + age);\n  }\n\n  public static void main(String[] args) {\n    myMethod();\n    myMethod2(\"Liam\", 5);\n    myMethod2(\"Jenny\", 8);\n  }\n}\n// - static means that the method belongs to the Main class and not an object of the Main class. You will learn more about objects and how to access methods through objects later in this tutorial.\n// - void means that this method does not have a return value. You will learn more about return values later in this chapter\n\n```\n\n## Switch\n\n```java\nswitch(expression) {\n  case x:\n    // code block\n    break;\n  case y:\n    // code block\n    break;\n  default:\n  // Run if there is no case match:\n    // code block\n}\n\n// The break keyword:\n// When Java reaches a break keyword, it breaks out of the switch block.\n\n// This will stop the execution of more code and case testing inside the block.\n\n// When a match is found, and the job is done, it's time for a break. There is no need for more testing.\n```\n\n## Two dimentional array\n\n```java\nint[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };\nint x = myNumbers[1][2];\nSystem.out.println(x); // Outputs 7\n```\n\n## arrays in java\n\n```\nString[] cars = {\"Volvo\", \"BMW\", \"Ford\", \"Mazda\"};\nSystem.out.println(cars[0]);\n// Outputs Volvo\n\nSystem.out.println(cars.length);\n// Outputs 4\n```\n\n## break/continue\n\nsrc: https://www.w3schools.com/java/java_break.asp\nJava Break\nYou have already seen the break statement used in an earlier chapter of this tutorial. It was used to \"jump out\" of a switch statement.\n\nThe break statement can also be used to jump out of a loop.\n\nJava Continue\nThe continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.\n\n## for each loop\n\nThere is also a \"for-each\" loop, which is used exclusively to loop through elements in an array:\n\n```java\nfor (type variableName : arrayName) {\n  // code block to be executed\n}\n\n// for e.g.,\nString[] cars = {\"Volvo\", \"BMW\", \"Ford\", \"Mazda\"};\n\nfor (String i : cars) {\n  System.out.println(i);\n}\n```\n\n## For loop\n\n```java\nfor (int i = 0; i \u003c 5; i++) {\n  System.out.println(i);\n}\n```\n\n## Ternary operator in java\n\n```java\n// Syntax:\n// variable = (condition) ? expressionTrue :  expressionFalse;\n\nint time = 20;\nif (time \u003c 18) {\n  System.out.println(\"Good day.\");\n} else {\n  System.out.println(\"Good evening.\");\n}\n\n// REDUCES TO:\nint time = 20;\nString result = (time \u003c 18) ? \"Good day.\" : \"Good evening.\";\nSystem.out.println(result);\n```\n\n## if/else, while, do/while\n\n```java\nwhile (condition) {\n  // code block to be executed\n}\n\n// do/while loop:\n// This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.\ndo {\n  // code block to be executed\n}\nwhile (condition);\n\nif (20 \u003e 18) {\n  System.out.println(\"20 is greater than 18\");\n}\n\nif (condition) {\n  // block of code to be executed if the condition is true\n} else {\n  // block of code to be executed if the condition is false\n}\n\nif (condition1) {\n  // block of code to be executed if condition1 is true\n} else if (condition2) {\n  // block of code to be executed if the condition1 is false and condition2 is true\n} else {\n  // block of code to be executed if the condition1 is false and condition2 is false\n}\n```\n\n## Modules importing and easeness\n\n```java\n// import org.springframework.web.bind.annotation.GetMapping;\n// import org.springframework.web.bind.annotation.RequestParam;\n// import org.springframework.web.bind.annotation.RestController;\n\n// So instead of above lines we can do like this:\nimport org.springframework.web.bind.annotation.*;\n// src: This line is a substitution for using multiple entities. Src: https://stackoverflow.com/a/39382541/10012446\n```\n\n## Booleans\n\n```java\nboolean isJavaFun = true;\nboolean isFishTasty = false;\nSystem.out.println(isJavaFun);     // Outputs true\nSystem.out.println(isFishTasty);   // Outputs false\n\n\nSystem.out.println(10 \u003e 9); // returns true, because 10 is higher than 9\n\n\nint x = 10;\nSystem.out.println(x == 10); // returns true, because the value of x is equal to 10\n```\n\n## Math functions:\n\n```java\nMath.max(5, 10);// to find the highest value of x and y\nMath.min(5, 10);// to find the lowest value of x and y:\nMath.sqrt(x); // square root.\nMath.abs(x); // method returns the absolute (positive) value of x i.e., -2 =\u003e 2 and 2 =\u003e 2\nMath.random(); // returns a random number between 0.0 (inclusive), and 1.0 (exclusive):\n\n// want a random number between 0 and 100, you can use the following formula:\nint randomNum = (int)(Math.random() * 101);  // 0 to 100\n// Complete reference of Math: https://www.w3schools.com/java/java_ref_math.asp\n```\n\n## String methods:\n\n```java\n// A String variable contains a collection of characters surrounded by double quotes:\nString txt = \"Hello World\";\nSystem.out.println(\"The length of the txt string is: \" + txt.length());\nSystem.out.println(txt.toUpperCase());   // Outputs \"HELLO WORLD\"\nSystem.out.println(txt.toLowerCase());   // Outputs \"hello world\"\n\n// Finding character in string:\nSystem.out.println(txt.indexOf(\"locate\")); // Outputs 7\n\n// Concatate without using + operator:\nString firstName = \"John\";\nString lastName = \"Doe\";\nSystem.out.println(firstName.concat(lastName));// JohnDoe\n\n// If you add a number and a string, the result will be a string concatenation:\nString x = \"10\";\nint y = 20;\nString z = x + y;   // z will be 1020 (a String)\n\n// Closest to template literal in java:\nSystem.out.printf(\"This is a template %s %s %s\", \"for\", \"an \", \"example\");\n// NOTE :  ^^^^^^^ this is printf not print or println.\n```\n\n## Java operators:\n\nAll operators are very similar to other langs like js, python and c.\nhttps://www.w3schools.com/java/java_operators.asp\n\n## Type casting ( converting type) in Java\n\nsrc: https://www.w3schools.com/java/java_type_casting.asp\n\n```java\n// Automatic Casting:\nint myInt = 9;\ndouble myDouble = myInt; // Automatic casting: int to double\n\n// Narrow Casting:\ndouble myDouble = 9.78d;\nint myInt = (int) myDouble; // Manual casting: double to int\n```\n\n## Using variables in java\n\nSrc: https://www.w3schools.com/java/java_variables.asp\n\n```txt\nString - stores text, such as \"Hello\". String values are surrounded by double quotes\nint - stores integers (whole numbers), without decimals, such as 123 or -123\nfloat - stores floating point numbers, with decimals, such as 19.99 or -19.99\nchar - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes\nboolean - stores values with two states: true or false\n```\n\n**Syntax**:\n\n```java\ntype variableName = value;\n\nint myNum = 5;\nfloat myFloatNum = 5.99f;\nchar myLetter = 'D';\nboolean myBool = true;\nString myText = \"Hello\";\n\nint x = 5, y = 6, z = 50;\n\nSystem.out.println(variableName);\n```\n\n## Primitive Data Types in Java\n\nsrc: https://www.w3schools.com/java/java_data_types.asp\n\nPrimitive Data Types\nA primitive data type specifies the size and type of variable values, and it has no additional methods.\n\nThere are eight primitive data types in Java:\n\n```txt\nData Type Size Description\nbyte 1 byte Stores whole numbers from -128 to 127\nshort 2 bytes Stores whole numbers from -32,768 to 32,767\nint 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647\nlong 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807\nfloat 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits\ndouble 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits\nboolean 1 bit Stores true or false values\nchar 2 bytes Stores a single character/letter or ASCII values\n```\n\n#### Final Variables/ Constants\n\nHowever, you can add the final keyword if you don't want others (or yourself) to overwrite existing values (this will declare the variable as \"final\" or \"constant\", which means unchangeable and read-only):\n\nExample\n\n```java\nfinal int myNum = 15;\nmyNum = 20; // will generate an error: cannot assign a value to a final variable\n```\n\n## Using json in java:\n\n1. Add below code to your pom.xml file:\n\nDocs (what I found): https://stleary.github.io/JSON-java/org/json/JSONObject.html\nDocs @ android: https://developer.android.com/reference/org/json/JSONObject\nDocs @ javadoc: https://www.javadoc.io/doc/org.json/json/20170516/org/json/class-use/JSONObject.html\n\nFYI: We can use google's library as well i.e.,\n`import com.google.gson.JsonObject;`\n\n```txt\n\u003c!-- src: https://stackoverflow.com/a/22876057/10012446 --\u003e\n\u003cdependency\u003e\n    \u003cgroupId\u003eorg.json\u003c/groupId\u003e\n    \u003cartifactId\u003ejson\u003c/artifactId\u003e\n    \u003cversion\u003e20211205\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n2. Then you can use it like:\n\n```java\nimport org.json.JSONObject;\nJSONObject obj = new JSONObject();\n\nobj.put(\"name\", \"foo\");\nobj.put(\"num\", new Integer(100));\nobj.put(\"balance\", new Double(1000.21));\nobj.put(\"is_vip\", new Boolean(true));\nSystem.out.print(obj);\n```\n\nFYI: Deprecations since Java 9: (i.e., Sept 27, 2017):\n\n```txt\nThe constructor Boolean(boolean) is deprecated\nThe constructor Byte(byte) is deprecated\nThe constructor Short(short) is deprecated\nThe constructor Character(char) is deprecated\nThe constructor Long(long) is deprecated\nThe constructor Float(float) is deprecated\nThe constructor Double(double) is deprecated\n```\n\nWhat does deprecation imply us to use now.?\n\nMore shit: https://stackoverflow.com/a/47095501/10012446\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsahilrajput03%2Flearning_java","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsahilrajput03%2Flearning_java","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsahilrajput03%2Flearning_java/lists"}