Java – OneCompiler – Write, run and share Java code online

Write, run, and share Java code online using OneCompiler’s Java online compiler for free. It is one of the robust and feature-rich online compilers for the Java language, running Java LTS version 17. Getting started with OneCompiler’s Java editor is quick and easy. The editor displays sample boilerplate code when you choose the language as Java and start coding.

OneCompiler’s Java online editor supports stdin and users can give inputs to programs using the STDIN text box on the I/O tab. Below is a sample program that shows reading STDIN ( A string in this case ).

import java.util.Scanner; class Input { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println(“Enter your name: “); String inp = input.next(); System.out.println(“Hello, ” + inp); } }

OneCompiler supports Gradle for dependency management. Users can add dependencies in the build.gradle file and use them in their programs. When you first add the dependencies, the first run may be a bit slow as we download the dependencies, but subsequent runs will be faster. The following sample Gradle configuration shows how to

add dependencies apply plugin:’application’ mainClassName = ‘HelloWorld’ run { standardInput = System.in } sourceSets { main { java { srcDir ‘./’ } } } } repositories { jcenter() } dependencies { // add dependencies here as below implementation group: ‘org.apache.commons’, name: ‘commons-lang3’, version: ‘3.9’ }

Java is a very popular general-purpose programming language, it is class-based and object-oriented. Java was developed by James Gosling at Sun Microsystems (later acquired by Oracle) the initial release of Java was in 1995. Java 17 is the last long-term supported version (LTS). As of today, Java is the world’s number one server programming language with a developer community of 12 million, 5 million students studying worldwide, and the #1 choice for cloud development.

Short variables

x = 999; -32768 to 32767 int x = 99999; -2147483648 to 2147483647 x length = 99999999999L; -9223372036854775808 to 9223372036854775807 float x = 1,2; double x = 99.99d; byte x = 99; -128 to 127 characters x = ‘A’; Boolean x = true;

Loops

1.

If Else:

When you want to perform a set of operations based on an If-Else condition is used. if(conditional-expression

) { // code } else { // code }

Example:

int i = 10; if(i % 2 == 0) { System.out.println(“i is even number”); } else { System.out.println(“i is odd number”); }

2

. Switch:

Switch is an alternative to the If-Else-If ladder and to select one among many blocks of code.

switch(<conditional-expression>) { case value1: // code break; // optional case value2: // code break; // optional … default: //code to be executed when all previous cases do not match; }

3. The

For

:For loop is used to iterate a set of instructions based on a condition. It is usually preferred for looping when the number of iterations is known in advance.

for(Initialization; Condition; increase/decrease){ //code }

4. While:While

is also used to iterate a set of instructions based on a condition. Usually while preferred when the number of iterations is not known in advance.

while(<condition>){ // code }

5. Do-While:

Do-while is also used to iterate a set of instructions based on a condition. It is mainly used when you need to execute the instructions at least once.

do { // code } while (<condition>);

Classes and objects

The class is the blueprint of an object, which is also known as a user-defined data type with variables and functions. Object is a basic unit in OOP and is an instance of the class.

How to create a keyword

Class

:class

is required to create a class.

Example

:class Mobile { public://access specifier specifying that the accessibility of class members string name; // string variable (attribute) int price; // int variable attribute };

How to create an object:

Mobile m1 = new Mobile();

How to define methods in a class:public class

Greeting { static void hello() { System.out.println(“Hello.. Happy learning!”); } public static void main(String[] args) { hello(); } }

The Collections collection

is a group of objects that can be represented as a single unit. Collections are introduced to provide a unified common interface to all objects.

Collection Framework was introduced since JDK 1.2 which is used to represent and manage collections and contains

:

  1. Interfaces
  2. Classes
  3. Algorithms

This framework also defines map interfaces and various classes in addition to collections

.

Advantages:

  • High performance
  • Reduces developer effort Unified architecture
  • that has common methods for all objects.

CollectionDescriptionSetSet is a collection of items that cannot contain duplicate values. Set is implemented in HashSets, LinkedHashSets, TreeSet etcListList is an ordered collection of items that may have duplicates. Lists are categorized in the ArrayList, LinkedList, VectorsQueueFIFO approach, while when creating an instance of the queue interface you can choose LinkedList or PriorityQueue.DequeDeque(Double Ended Queue) is used to add or remove items from both ends of the queue (both head and tail)MapMap contains key-value pairs that have no duplicates. The map is implemented in HashMap, TreeMap, etc.