본문 바로가기

공부

String객체 이용하기 A) String s = new String("abc"); B) String s = "abc"; - B와 A는 같습니다. - 원래 A처럼 써야 하는데, 워낙 사용빈도가 많아서 B처럼 써도 컴파일러가 알아서 A처럼 바꿔서 컴파일 합니다. - 메모리는 Stack Memory와 Heap Memory가 있는데요. Stack에는 주소값(Address)만 저장이 됩니다.(앗! 주소값 이외에 int/byte 등 약속된 타입은 저장이 되는 군요) - new String("abc"); 하면 Head메모리에 String객체가 생성되고, 그 객체에는 'abc'를 갖게 됩니다. - '='기호는 Assign Operator(맞나?^^;;)이라고 하며, '='표시 우측의 값을 왼쪽에 넣는 행동을 하게 됩니다. - 따라서 Heap.. 더보기
열혈강의 자바 455쪽 import java.io.*; public class Mains { public static void main(String[] args) { DataInputStream dis1 = null; try{ dis1 = new DataInputStream( new BufferedInputStream( new FileInputStream( new File(new File("c:\\java"), "abc.txt")))); } catch(FileNotFoundException fvfe){} int a = 0; double b = 0.0; byte[] c = null; try{ a = dis1.readInt(); b = dis1.readDouble(); c = new byte[10]; dis1.read(c); di.. 더보기
열혈강의 자바 454쪽 import java.io.*; public class Mains { public static void main(String[] args) throws IOException { DataOutputStream dos1 = new DataOutputStream( new BufferedOutputStream( new FileOutputStream( new File(new File("c:\\java"), "abc.txt")))); dos1.writeInt(23); dos1.writeDouble(12.345); dos1.writeBytes("ABCDEFG!!"); dos1.close(); } } 더보기
열혈강의 자바 453쪽 import java.io.*; public class Mains { public static void main(String[] args) throws FileNotFoundException, IOException { File file = new File("c:\\java\\abc.txt"); FileInputStream fis = new FileInputStream(file); byte[] by = new byte[65536]; int count = fis.read(by); for(int i = 0; i 더보기
열형강의 자바 452쪽 import java.io.*; public class Mains { public static void main(String[] args) { File file = new File("c:\\java\\abc.txt"); try{ FileOutputStream fos = new FileOutputStream(FileDescriptor.out); FileOutputStream fos1 = new FileOutputStream(file); byte[] data = {66, 68, 70, 72, (byte)'!'}; fos.write(data); fos1.write(data); } catch(FileNotFoundException fnfe){ System.out.println("파일을 못 찾겠다"); Syste.. 더보기
자바 - 한파일 안에 파일이름과 같은 클래스만 public를 붙일 수 있다 한 파일안에 클래스가 여러개 있다면 파일이름과 같은 클래스에만 public를 붙일 수 있다 더보기
Run 을 눌렀는데 Selection does not contain a main type에러, 클래스 안의 다른클래스변수와 다른클래스객체 생성하여 사용 1. Run 을 눌렀는데 Selection does not contain a main type에러 -> public static void main(String[] args) 정확히 쓸것 2. (1) import Table.Table; class ds{ Table a; } public class Mains{ public static void main(String[] args) { ds ba = new ds(); ba.a = new Table(); ba.a.c = 0; System.out.println(ba.a.c); } } 클래스 안에 다른 클래스변수만 선언해놓고 main함수에서 다른 클래스변수만 선언해 놓은 것을 객체를 생성해서 사용가능 (2) import Table.Table; class ds{ Tab.. 더보기
자바 - The public type [class name] must be defined in its own file 에러 이 에러는 클래스 이름이 파일명과 일치 하지 않을 때 와 하나의 파일에 여러 개의 클래스가 있을 때 public 클래스가 있을 때 발생하는 에러이다. 하나의 파일에 클래스가 여러 개 있을 때 public 을 붙일수 있는 클래스는 파일명과 일치하는 클래스명이다. 그 이외의 클래스는 public 삭제한다. 하지만 아래와 같이 파일명과 일치하는 클래스 파일이 다른 클래스를 포함하고 있을때는 public 을 사용해도 상관없다. 더보기