成都创新互联网站制作重庆分公司

编写java代码声明,Java代码编写

写出java代码,如何声明数组,分配内存给数组,并给数组指定初始值

java 里没有c中的malloc,只有new关键字会分配内存。

站在用户的角度思考问题,与客户深入沟通,找到法库网站设计与法库网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站设计、做网站、企业官网、英文网站、手机端网站、网站推广、申请域名、网页空间、企业邮箱。业务覆盖法库地区。

primitive types(int, float, double, char, boolean, byte)

分步:

int[] array // 此时jvm未分配内存

array = new int[2]; //此时分配内存,2个int

一步:

int[] array = {1,2}

object types (Object)

分步:

Object[] objs; // 此时jvm未分配内存

objs = new Object[2]; // 此时jvm分配了数组本身用的内存,但数组内元素内存未分配。

objs[0] = new Object(); // 此时分配内存

objs[1] = new Object(); // 此时分配内存

一步:

Object[] objs = {new Object(), new Object()};

JAVA父类及子类的声明编程题(编写的代码行后面要配有必要的注释)

程序如下供你参考.说明:

1.0 你题目中只提供了两门成绩,英语和计算机,但是下面说三门.

如果是的话,自己添加上去吧.很好修改的.

2.0 如果有什么不清楚的话,补充问题吧.或者可以给你留言.

3.0 希望你自己多看书把语言的基础知识学好.

下面共四个java类文件.

/***Student.java **/

public class Student {

protected int studentID;

protected String name;

protected int englishScore;

protected int computerScore;

protected int totalScore;

public Student(int studentID, String name) {

super();

this.studentID = studentID;

this.name = name;

}

public Student(int studentID, String name, int englishScore,

int computerScore) {

super();

this.studentID = studentID;

this.name = name;

this.englishScore = englishScore;

this.computerScore = computerScore;

}

public int getStudentID() {

return studentID;

}

public void setStudentID(Integer studentID) {

this.studentID = studentID;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getEnglishScore() {

return englishScore;

}

public void setEnglishScore(int englishScore) {

this.englishScore = englishScore;

}

public int getComputerScore() {

return computerScore;

}

public void setComputerScore(int computerScore) {

this.computerScore = computerScore;

}

public int getTotalScore() {

return totalScore;

}

/**totalScore 没有set 方法 */

@Override

public String toString() {

return "Student [studentID=" + studentID + ", name=" + name

+ ", englishScore=" + englishScore + ", computerScore="

+ computerScore + ", totalScore=" + totalScore + "]";

}

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

Student other = (Student) obj;

if (name == null) {

if (other.name != null)

return false;

} else if (!name.equals(other.name))

return false;

if (studentID != other.studentID)

return false;

return true;

}

/** @return 返回1 表示 大于,返回 0 表示等于, 返回-1表示小于 */

public int compare(Student other){

if(totalScore other.totalScore)

return -1; //

if(totalScore == other.totalScore)

return 0;

return 1;

}

private void sum(){

totalScore = englishScore+computerScore;

}

/**计算评测成绩 */

public double testScore(){

return (englishScore+computerScore)/2;

}

}

/***** StudentXW.java******/

public class StudentXW extends Student {

private String response;///责任

public StudentXW(int studentID, String name, String response) {

super(studentID, name);

this.response = response;

}

public StudentXW(int studentID, String name, int englishScore,

int computerScore, String response) {

super(studentID, name, englishScore, computerScore);

this.response = response;

}

public String getResponse() {

return response;

}

public void setResponse(String response) {

this.response = response;

}

@Override

public double testScore(){

return (englishScore+computerScore)/2+3;

}

}

/*****StudentBZ.java****/

public class StudentBZ extends Student {

private String response;///责任

public StudentBZ(int studentID, String name, String response) {

super(studentID, name);

this.response = response;

}

public StudentBZ(int studentID, String name, int englishScore,

int computerScore, String response) {

super(studentID, name, englishScore, computerScore);

this.response = response;

}

public String getResponse() {

return response;

}

public void setResponse(String response) {

this.response = response;

}

@Override

public double testScore(){

return (englishScore+computerScore)/2+5;

}

}

/*****测试类 TestStudent.java****/

/** Student ,StudentXW, 及StudentBZ测试类 */

public class TestStudent {

public static void main(String[] args) {

Student s1 = new Student(1,"one");

s1.setEnglishScore(80);

s1.setComputerScore(80);

StudentXW sx1 = new StudentXW(2,"xone","学习委员");

sx1.setEnglishScore(90);

sx1.setComputerScore(90);

StudentBZ sb1 = new StudentBZ(3,"bone","班长");

sb1.setEnglishScore(70);

sb1.setComputerScore(70);

System.out.println("学生 One 的评测成绩为:"+s1.testScore());

System.out.println("学生 xone,学习委员的评测成绩为:"+sx1.testScore());

System.out.println("学生 bone,班长的评测成绩为:"+sb1.testScore());

Student [] studentArray = new Student[5];

studentArray[0] = new Student(101,"studentOne",60,60);

studentArray[1] = new Student(102,"studentTwo",65,65);

studentArray[2] = new Student(103,"studentThree",70,70);

studentArray[3] = new StudentXW(104,"studentXW",75,75,"学习委员");

studentArray[4] = new StudentBZ(104,"studentXW",80,80,"学习委员");

for(Student student:studentArray){

System.out.println("名字为:"+student.getName()+"的成绩为:"+ student.testScore());

}

}

}

想编写优美的java代码格式要记住这几条规则

做到这些规则的目的很简单,就是写出“优美”的Java代码来。

1、Java注释尽可能全面

对于方法的注释应该包含详细的入参和结果说明,有异常抛出的情况也要详细叙述:类的注释应该包含类的功能说明、作者和修改者。

2、多次使用的相同变量最好归纳成常量 多处使用的相同值的变量应该尽量归纳为一个常量,方便日后的维护。

3、尽量少的在循环中执行方法调用 尽量在循环中少做一些可避免的方法调用,这样可以节省方法栈的创建。例如:

for(int i=0;ilist.size();i++){

System.out.println(i);}可以修改为:

for(int i=0,size=list.size();isize;i++){

System.out.println(i);}4、常量的定义可以放到接口中 在Java培训中,接口里只允许存在常量,因此把常量放到接口中声明就可以省去public static final这几个关键词。

5、ArrayList和LinkedList的选择 这个问题比较常见。通常程序员最好能够对list的使用场景做出评估,然后根据特性作出选择。ArrayList底层是使用数组实现的,因此随机读取数据 会比LinkedList快很多,而LinkedList是使用链表实现的,新增和删除数据的速度比ArrayList快不少。

6、String,StringBuffer和StringBuilder 这个问题也比较常见。在进行字符串拼接处理的时候,String通常会产生多个对象,而且将多个值缓存到常量池中。例如:

String a=“a”;

String b=“b”;a=a+b;这种情况下jvm会产生“a”,“b”,“ab”三个对象。而且字符串拼接的性能也很低。因此通常需要做字符串处理的时候尽量采用StringBuffer和StringBuilder来。

7、包装类和基本类型的选择 在代码中,如果可以使用基本数据类型来做局部变量类型的话尽量使用基本数据类型,因为基本类型的变量是存放在栈中的,包装类的变量是在堆中,栈的操作速度比堆快很多。

8、尽早的将不再使用的变量引用赋给null 这样做可以帮助jvm更快的进行内存回收。当然很多人其实对这种做法并不感冒。


网站栏目:编写java代码声明,Java代码编写
本文链接:http://cxhlcq.com/article/hsopcp.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部