Coder Social home page Coder Social logo

java's Introduction

Java Language Basics

Syntax

Java syntax is similar to C and C++. It uses semicolons to end statements and curly braces {} to define blocks of code.

Printing in Java

Printing output in Java is typically done using System.out.println() or System.out.print() methods.

  public class HelloWorld {
      public static void main(String[] args) {
          System.out.println("Hello, World!");
      }
  }

Variables in Java

Java variables must be declared with a specific type. Common types include int, double, boolean, String, etc.

  int age = 25;
  double price = 19.99;
  boolean isActive = true;
  String name = "John";

Loops

Java supports for, while, and do-while loops.

  // For loop
  for (int i = 1; i <= 5; i++) {
      System.out.println(i);
  }
  
  // While loop
  int count = 1;
  while (count <= 5) {
      System.out.println(count);
      count++;
  }
  
  // Do-while loop
  int num = 1;
  do {
      System.out.println(num);
      num++;
  } while (num <= 5);

Functions (Methods)

Functions in Java are called methods. They are defined within classes and can be static (class-level) or instance methods

  public class MyClass {
      // Static method
      public static void sayHello() {
          System.out.println("Hello, World!");
      }
  
      // Instance method
      public void greet(String name) {
          System.out.println("Hello, " + name);
      }
  
      public static void main(String[] args) {
          sayHello(); // Calling a static method
          MyClass obj = new MyClass();
          obj.greet("John"); // Calling an instance method
      }
  }

Returning Values

Methods in Java can return values using the return keyword.

  public class Calculator {
      public int add(int a, int b) {
          return a + b;
      }
  
      public static void main(String[] args) {
          Calculator calc = new Calculator();
          int result = calc.add(5, 3);
          System.out.println("Sum: " + result);
      }
  }

Use of Lists (Arrays and ArrayLists)

Java arrays are fixed-size collections of elements of the same type. ArrayLists are dynamically resizable lists.

  // Arrays
  int[] numbers = {1, 2, 3, 4, 5};
  System.out.println("First number: " + numbers[0]);
  
  // ArrayLists
  import java.util.ArrayList;
  ArrayList<String> names = new ArrayList<>();
  names.add("Alice");
  names.add("Bob");
  System.out.println("First name: " + names.get(0));

java's People

Contributors

cyber-programer avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.