首页 > 编程语言 >Java编程从入门到放弃

Java编程从入门到放弃

时间:2024-07-03 14:56:21浏览次数:16  
标签:Java 入门 void 编程 System public println class out

1.配置开发环境

安装JDK

官网下载地址: https://www.oracle.com/java/technologies/downloads/

配置环境变量

最新版本JDK22无需手动配置环境变量。

老版本: 此电脑-右键属性-高级系统设置-环境变量-系统变量-Path-编辑 C:\Java\jdk1.8.0_65\bin

检查结果 java -version

Hello World

//HelloWorld.java
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}
# 编译
javac HelloWorld.java
# 执行
java HelloWorld

安装 IntelliJ IDEA

下载地址: https://www.jetbrains.com/idea/download/?section=windows

2. 基础语法

2.1 数据类型

基本数据类型

Data Type Size Description
byte 1 byte -128 to 127
short 2 bytes -32,768 to 32,767
int 4 bytes -2,147,483,648 to 2,147,483,647
long 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits
boolean 1 bit true or false
char 2 bytes Stores a single character/letter or ASCII values

类型转换

  1. 自动类型转换
    byte -> short -> char -> int -> long -> float -> double

  2. 手动类型转换
    double -> float -> long -> int -> char -> short -> byte

数组

String[] cars = {"Volvo", "BMW", "Ford"};

2.2 流程控制

If ... Else

if (condition) {
  // block of code to be executed if the condition is true
} else {
  // block of code to be executed if the condition is false
}

// 三元运算符
variable = (condition) ? expressionTrue :  expressionFalse;

Switch

switch(expression) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
}

While Loop

while (condition) {
  // code block to be executed
}

do {
  // code block to be executed
}
while (condition);

For Loop

for (statement 1; statement 2; statement 3) {
  // code block to be executed
}

// For-Each
for (type variableName : arrayName) {
  // code block to be executed
}

2.3 函数方法

public class Main {
  static void myMethod() {
    System.out.println("I just got executed!");
  }

  public static void main(String[] args) {
    myMethod();
  }
}

3. 面向对象

3.1 类与对象

属性与方法

public class Main {
  int x = 5; // 属性
  // 方法
  static void myMethod() {
    System.out.println("Hello World!");
  }

  public static void main(String[] args) {
    Main myObj1 = new Main();
    myObj1.x = 25;
    System.out.println(myObj1.x);
    myMethod();
  }
}

构造函数

public class Main {
  int x;

  public Main(int y) {
    x = y;
  }

  public static void main(String[] args) {
    Main myObj = new Main(5);
    System.out.println(myObj.x);
  }
}

修饰符

访问修饰符

For classes, you can use either public or default:

Modifier Description
public The class is accessible by any other class
default The class is only accessible by classes in the same package

For attributes, methods and constructors, you can use the one of the following:

Modifier Description
public The code is accessible for all classes
private The code is only accessible within the declared class
default The code is only accessible in the same package
protected The code is accessible in the same package and subclasses
非访问修饰符

For classes, you can use either final or abstract:

Modifier Description
final The class cannot be inherited by other classes
abstract The class cannot be used to create objects

For attributes and methods, you can use the one of the following:

Modifier Description
final Attributes and methods cannot be overridden/modified
static Attributes and methods belongs to the class, rather than an object
abstract Can only be used in an abstract class, and can only be used on methods
transient Attributes and methods are skipped when serializing the object containing them
synchronized Methods can only be accessed by one thread at a time
transient The value of an attribute is not cached thread-locally, and is always read from the "main memory"

3.2 封装

public class Person {
  private String name; // private = restricted access

  // Getter
  public String getName() {
    return name;
  }

  // Setter
  public void setName(String newName) {
    this.name = newName;
  }
}

3.2 继承

class Vehicle {
  protected String brand = "Ford";        // Vehicle attribute
  public void honk() {                    // Vehicle method
    System.out.println("Tuut, tuut!");
  }
}

class Car extends Vehicle {
  private String modelName = "Mustang";    // Car attribute
  public static void main(String[] args) {

    // Create a myCar object
    Car myCar = new Car();

    // Call the honk() method (from the Vehicle class) on the myCar object
    myCar.honk();

    // Display the value of the brand attribute (from the Vehicle class) and the value of the modelName from the Car class
    System.out.println(myCar.brand + " " + myCar.modelName);
  }
}

3.3 多态

class Animal {
  public void animalSound() {
    System.out.println("The animal makes a sound");
  }
}

class Pig extends Animal {
  public void animalSound() {
    System.out.println("The pig says: wee wee");
  }
}

class Dog extends Animal {
  public void animalSound() {
    System.out.println("The dog says: wow wow");
  }
}

class Main {
  public static void main(String[] args) {
    Animal myAnimal = new Animal();  // Create a Animal object
    Animal myPig = new Pig();  // Create a Pig object
    Animal myDog = new Dog();  // Create a Dog object
    myAnimal.animalSound();
    myPig.animalSound();
    myDog.animalSound();
  }
}

3.4 抽象

抽象类和方法

// Abstract class
abstract class Animal {
  // Abstract method (does not have a body)
  public abstract void animalSound();
  // Regular method
  public void sleep() {
    System.out.println("Zzz");
  }
}

// Subclass (inherit from Animal)
class Pig extends Animal {
  public void animalSound() {
    // The body of animalSound() is provided here
    System.out.println("The pig says: wee wee");
  }
}

class Main {
  public static void main(String[] args) {
    Pig myPig = new Pig(); // Create a Pig object
    myPig.animalSound();
    myPig.sleep();
  }
}

4. 进阶

多线程

class ThreadDemo extends Thread {
   private Thread t;
   private String threadName;
   
   ThreadDemo( String name) {
      threadName = name;
      System.out.println("Creating " +  threadName );
   }
   
   public void run() {
      System.out.println("Running " +  threadName );
      try {
         for(int i = 4; i > 0; i--) {
            System.out.println("Thread: " + threadName + ", " + i);
            // 让线程睡眠一会
            Thread.sleep(50);
         }
      }catch (InterruptedException e) {
         System.out.println("Thread " +  threadName + " interrupted.");
      }
      System.out.println("Thread " +  threadName + " exiting.");
   }
   
   public void start () {
      System.out.println("Starting " +  threadName );
      if (t == null) {
         t = new Thread (this, threadName);
         t.start ();
      }
   }
}
 
public class TestThread {
 
   public static void main(String args[]) {
      ThreadDemo T1 = new ThreadDemo( "Thread-1");
      T1.start();
      
      ThreadDemo T2 = new ThreadDemo( "Thread-2");
      T2.start();
   }   
}

标签:Java,入门,void,编程,System,public,println,class,out
From: https://www.cnblogs.com/rustling/p/18281606

相关文章

  • ArcGIS API for Javascript解决html2canvas、domtoimage截图地图出现空白问题
    原因使用html2canvas、domtoimage进行截图时,会出现地图面板是空白的情况,报错如下:#1133msUnabletocloneWebGLcontextasithaspreserveDrawingBuffer=false<canvasstyle=​"width:​100%;​height:​100%;​>在通过ArcGISAPIforJavaScript4.X版本实例化地图的......
  • 【2024版】最新HW参考 | HVV行动之蓝军经验总结(非常详细)零基础入门到精通,收藏这一篇就
    ‍正文:HW行动,攻击方的专业性越来越高,ATT&CK攻击手段覆盖率也越来越高,这对于防守方提出了更高的要求,HW行动对甲方是一个双刃剑,既极大地推动了公司的信息安全重视度和投入力量,但同时对甲方人员的素质要求有了很大提升,被攻破,轻则批评通报,重则岗位不保;大的金融、央企可能不担心......
  • 多模态大模型入门指南
    作者:林夕,阿里巴巴集团高级算法工程师,专注多模态大模型研究。声明:本文只做分享,版权归原作者,侵权私信删除!原文:https://zhuanlan.zhihu.com/p/682893729内容总结,本篇综述主要介绍和分析了以下几个方面:•概述了MM-LLMs的设计形式,将模型架构分为5个部分:模态编码器、输入......
  • C#面:编程长度为10000的字符串,通过随机从a-z中抽取10000个字符组成
    使用C#中的Random类来生成随机字符,并使用StringBuilder类来构建字符串。以下是一个示例程序:usingSystem;usingSystem.Text;classProgram{staticvoidMain(){Randomrandom=newRandom();StringBuilderstringBuilder=newStringBuild......
  • Java 流式编程详解,Demo案例解析
    Java流式编程详解,Demo案例解析JavaStreams在很多年前就被引入了,但作为Java开发者,我们还没有完全掌握这个多功能工具的威力。在这里,你将发现一些有价值的技巧,可以作为参考并应用到你的下一个项目中。在下面的示例中,我们将使用以下类。@GetterclassCompany{privat......
  • 基于Java的宠物领养管理系统
    目录        一、系统简介1.需求分析2 .编程环境与工具二、系统总体设计1.系统的功能模块图2.系统架构3.系统部署和测试步骤4.各功能模块简介(1)主页:(2)宠物知识:(3)领养中心:(4)团队博客:(5)团队展示:(6)注册/登录:三、主要业......
  • 【Java完整版 面试必备】Leetcode Top100题目和答案-矩阵篇
    目录以下摘自leetcodeTop100精选题目-矩阵篇​矩阵置零螺旋矩阵旋转图像搜索二维矩阵II以下摘自leetcodeTop100精选题目-矩阵篇矩阵置零给定一个 mxn 的矩阵,如果一个元素为 0 ,则将其所在行和列的所有元素都设为 0 。请使用 原地 算法。示例:输入:matrix......
  • JAVA妇产科专科电子病历系统源码,前端框架:Vue,ElementUI
    JAVA妇产科专科电子病历系统源码,前端框架:Vue,ElementUI孕产妇健康管理信息管理系统是一种将孕产妇健康管理信息进行集中管理和存储的系统。通过建立该系统,有助于提高孕产妇健康管理的效率和质量,减少医疗事故发生的可能性,管理医疗资源,保证孕产妇得到及时、准确的医疗服务。该系......
  • java的常用技术
    1、java集合(Iterable、List、Set、Map,JUC安全性集合)2、hashmap(原理,延申)、ConcurrentHashMap(锁:1.8是synchronized+node,1.7是segment)3、乐观锁(比较/交换)AtomicInteger是Java中的一个原子类4、悲观锁synchronized5、线程池运行状态运行过程其他......
  • 思考如何学习一门编程语言?
    一、什么是编程语言编程语言是一种用于编写计算机程序的人工语言。通过编程语言,程序员可以向计算机发出指令,控制计算机执行各种任务和操作。编程语言由一组语法规则和语义规则组成,这些规则定义了如何编写代码以及代码的含义。编程语言的基本组成部分语法(Syntax):语法......